Advertisement
Guest User

Untitled

a guest
Feb 7th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. public abstract class Array
  2. : ICloneable, IList, ICollection, IEnumerable {
  3.  
  4. public partial class Array<T>
  5. : ICloneable, IList<T>, ICollection<T>, IEnumerable<T> {
  6.  
  7. public partial class Array: Array<object> {
  8.  
  9. Mammoth[] mammoths = new Mammoth[10];
  10. Animal[] animals = mammoths; // Covariant conversion
  11. animals[1] = new Giraffe(); // Run-time exception
  12.  
  13. Array<Mammoth> mammoths = new Array<Mammoth>(10);
  14. Array<Animal> animals = mammoths; // Not allowed.
  15. IEnumerable<Animals> animals = mammoths; // Covariant conversion
  16.  
  17. ICollection<Mammoth> collection = new Mammoth[10]; // Cast to interface type
  18. collection.Add(new Mammoth()); // Run-time exception
  19.  
  20. ICollection<Mammoth> mammoths = new Array<Mammoth>(10);
  21. ICollection<Animal> animals = mammoths; // Not allowed
  22.  
  23. interface IList<out T> : ICollection<T>
  24. {
  25. T this[int index] { get; }
  26. int IndexOf(object value);
  27. }
  28.  
  29. interface ICollection<out T> : IEnumerable<T>
  30. {
  31. int Count { get; }
  32. bool Contains(object value);
  33. }
  34.  
  35. Array<Mammoth> mammoths = new Array<Mammoth>(10);
  36. IList<Animals> animals = mammoths; // Covariant conversion
  37.  
  38. Array ary = Array.Copy(.....);
  39. int[] values = (int[])ary;
  40.  
  41. for (Foo *foo = beginArray; foo != endArray; ++foo)
  42. {
  43. // use *foo -> which is the element in the array of Foo
  44. }
  45.  
  46. int[,] foo2 = new int[2, 3];
  47. foreach (var type in foo2.GetType().GetInterfaces())
  48. {
  49. Console.WriteLine("{0}", type.ToString());
  50. }
  51.  
  52. Array array = foo2;
  53. Console.WriteLine("Length = {0},{1}", array.GetLength(0), array.GetLength(1));
  54.  
  55. array.GetType().GetMethod("GetLength").Invoke(array, 0); // don't...
  56.  
  57. ((Array)someArray).GetLength(0); // do!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement