More about calling interface methods

  1. Which methods are possible to call?
  2. Calling interface methods that were implemented by value type.

We can call any method of the object which is declared by its type. In addition, we can call System.Object methods, because the CLR knows that the instance of an object is derived from System.Object.

 

Examples:

        static void Main(string[] args)

        {

            String str = "Hello world";

 

            //We can call any method of the object which is declared by its type,

            //in addition we can call System.Object methods, because the CLR knows

            //that String is derived from System.Object.

            IComparable comparable = str;

            Console.WriteLine(comparable.CompareTo(str));

            Console.WriteLine(comparable);

 

            //It is possible to cast comparable object to another object and call its method

            //that is refer to the same string object in the managed heap

            ICloneable cloneable = (ICloneable)comparable;

            Console.WriteLine(cloneable.Clone());

 

            Console.ReadKey();

        }

It is possible to cast comparable object to another object and call its methods because variable is refer to the same string object in the managed heap.

Calling of value type object method means that the instance of the object must be boxed, because the CLR needs to examine type of the object to call the proper method.   

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s