I am studying the Array class in C#. The following is the implementation of System.Array class:

public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable 

I have two questions regarding this:

  1. I have read that System.Array class is derived from System.Object. But why is this not seen in the above definition?

  2. ICloneable is implemented in order to allow the Array to be Deep-Copied in case of XML serialization. ICloneable provides only one method - Clone(). What is the advantage of implementing ICloneable explicitly? Why could not Clone() method be directly implemented? Is this a type of providing a Marking Interface pattern where a particular Interface is implemented only to convey a certain characteristic of the class?

  • 3
    It is not seen because every object derives from Object. It is implicit. – Oded Feb 18 '13 at 10:27
  • @Oded, If it is not seen, then it should be specified somewhere, right? It is somewhat confusing... Surely the fact that every object derives from Object would not be the only reason... Don't you think? – TheSilverBullet Feb 18 '13 at 10:38
  • 2
    It is documented that every type is derived from Object. – Oded Feb 18 '13 at 10:41
  • 2
    If you disassemble into IL code, you will see that System.Array extends from System.Object. As Oded points out, this information is implicit in higher level languages such as C# & VB. "If it is not seen, then it should be specified somewhere, right?" - Yes, it is specified in the IL. – MattDavey Feb 18 '13 at 11:04
  • 2
    @TheSilverBullet It's a given that everything extends from System.Object. The compiler allows you to omit specifying it because you can't prevent it. – Ross Patterson Feb 18 '13 at 12:48
  1. System.Array is effectively derived from System.Object. You don't see it here, because everything in .NET Framework derives by default from System.Object, including types like int (System.Int32).

    In other words:

    • class MyClass : MyOtherClass { } indicates that MyClass is derived from MyOtherClass.

    • class MyClass { } indicates that MyClass is derived from System.Object.

  2. The advantage of using an interface is that methods (or classes with generics) may rely on interfaces when they need a specific method or property. Imagine the following source code:

    public interface INamed
        string Name { get; }
    public void SayHello(INamed namedEntity)
        Console.WriteLine("Hello, " + namedEntity.Name + "!");

    By using an interface, the method shows that it doesn't care about the exact type of namedEntity. All what it cares about is that the object should contain a property of type string called Name which has a getter.

    The actual type may be a Person. Or a Cat : Animal. Or an AircraftModel. SayHello accepts them all, at the condition for them to implement INamed.

    Why would Person implement an interface, instead of simply containing the Name property?

    Because, aside using Reflection or dynamic programming, the caller doesn't have a clue that Person effectively implements Name property. For example this code will not compile:

    public class Person
        public string Name { get; set; }
    public void SayHello(object namedEntity)
        Console.WriteLine("Hello, " + namedEntity.Name + "!"); // Compile-time error here
    public void Main()
        this.SayHello(new Person { Name = "Robert" });
  • Just rephrasing so that I am sure I understood. For point 1, the very fact that Array is a class means that it is derived from System.Object. For point 2, as assumed, the idea behind implementng ICloneable is to "tell" the user that the class implements Clone() function. Am I correct? – TheSilverBullet Feb 18 '13 at 11:15
  • 1
    For the point 2, that's correct. For the point 1, a class may be derived from another class, which in turn derives for a third class, etc., but finally, the Nth class will derive from System.Object. – Arseni Mourzenko Feb 18 '13 at 11:19
  • @TheSilverBullet "to "tell" the user that the class implements Clone()" Not quite. It's to allow you to write methods that use any cloneable object without caring what kind of object it is. – Ross Patterson Feb 18 '13 at 12:52
  • 1
    @TheSilverBullet its not so much telling the user the class implements Clone(), rather its giving the user the option to create the method to take any class that implements the IClonable interface. Since it is reference by the interface, the method does not care what class is being passed since it only cares about the methods that the IClonable interface provides. – DFord Feb 18 '13 at 13:14
  • @RossPatterson, I get the perspective now. This looks like a facilitator for familiar design pattern. (Strategy?) – TheSilverBullet Feb 19 '13 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.