C# .NET - How override method gets called - Asked By anbu n on 18-Jan-12 12:13 AM

class CoOrds
{
public int x, y;


// Default constructor:
public CoOrds()
{
x = 0;
y = 0;
}

// tcA constructor with two arguments:
public CoOrds(int x, int y)
{
this.x = x;
this.y = y;
}

// Override the ToString method:
public override string ToString()
{
return (String.Format("({0},{1})", x, y));
}
}

class MainClass
{
static void Main()
{
CoOrds p1 = new CoOrds();
CoOrds p2 = new CoOrds(5, 3);

// Display the results using the overriden ToString method:
Console.WriteLine("CoOrds #1 at {0}", p1);
Console.WriteLine("CoOrds #2 at {0}", p2);
Console.ReadKey();
}
}
---------------------------------------
in the above code , how does "public override string ToString()" gets called
can any body please explain, when the debug comes to Console.WriteLine the "public override string ToString()" gets called
Sreekumar P replied to anbu n on 18-Jan-12 12:26 AM
Hi,

Quick Ans:

The ToString() method will me implicitly called when the "Console.WriteLine("CoOrds #1 at {0}", p1);" line is called.
Because "CoOrds" is a Object and any object that Override "ToString()" will to implicitly called over this areas of String operations.


Explained Ans:

All types in .Net inherit from system.object directly or indirectly. Because of this inheritance, every type in .Net inherit the ToString() method from System.Object class. Consider the example below.

using System;
public class MainClass
{
  public static void Main()
  {
   int Number = 10;
   Console.WriteLine(Number.ToString());
  }
}


In the above example Number.ToString() method will correctly give the string representaion of int 10, when you call the ToString() method.

If you have a Customer class as shown in the below example and when you call the ToString() method the output doesnot make any sense. Hence you have to override the ToString() method, that is inherited from the System.Object class.

using System;
public class Customer
{
 public string FirstName;
 public string LastName;
}
public class MainClass
{
 public static void Main()
 {
  Customer C = new Customer();
  C.FirstName = "David";
  C.LastName = "Boon";
  Console.WriteLine(C.ToString());
 }
}


 
Riley K replied to anbu n on 18-Jan-12 12:39 AM


ToString is a virtual method. In order to override a virtual method in C# you need to specify the override keyword


Every class or struct in C# implicitly inherits the http://msdn.microsoft.com/en-us/library/system.object.aspx class. Therefore, every object in C# gets the http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx method, which returns a string representation of that object

When you create a custom class or struct, you should override the http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx method in order to provide information about your type to client code.
Here is a good eg from MSDN

http://msdn.microsoft.com/en-us/library/ms173154.aspx

Regards
dipa ahuja replied to anbu n on 18-Jan-12 03:31 AM
Polymorphism which is also called method/operator overloading.
Means creating more than one function which has same name but different signature .
 
There are two types of polymorphism overloading and overriding.
 
Overloading : creating different methods with same name but different signature in the SAME CLASS.
 
Overriding : Creating the a method which has same name and same signature as the method of Parent Class
 
In your example you have taken the concept of overriding.
 
namespace ConsoleApplication1
{
  class Program
  {
  static void Main(string[] args)
  {
    A a1 = new A();
    b b1 = new b();
 
    a1.disp(); //Call From A
    b1.disp(); //Call From B
 
    a1 = new b();
    a1.disp(); // Call From B with the instance of A
 
    Console.ReadLine();
 
  }
  }
  public class A
  {
  public virtual void disp()
  {
    Console.WriteLine("called from A");
  }
  }
  public class b : A
  {
  public override void disp()
  {
    Console.WriteLine("called from B");
  }
  }
}
 
 
Hope you get it :)