please explain that program.why how compile move and what is the reason that in overriden function "ToString" the statement within Console.WriteLine("ToString " ) is not exicuted.
a.cs
public class zzz
{
public static void Main()
{
yyy a = new yyy(10);
System.Console.WriteLine(a);
}
}
public class yyy
{
public int i;
public yyy( int j)
{
i = j;
}
public static implicit operator string(yyy y)
{
System.Console.WriteLine("operator string");
return "string " + y.i;
}
public override string ToString()
{
System.Console.WriteLine("ToString");
return "mukhi";
}
}
Output
operator string
string 10
VulpesPosted Jul 24, 2012, 10:12 AM
ToString
mukhi
then you'd need to change this line:
to this:
As there's an implicit conversion from yyy to string, then the Console.WriteLine(string) overload is called rather than Console.WriteLine(object). If the latter had been called, then the ToString() method would have been called first on the object and then the result written to the console.
siddharth raiPosted Jul 24, 2012, 10:18 AM
Santhosh Kumar JayaramanPosted Jul 24, 2012, 9:09 AM
operator string
string 10
is it is there inside static method. Static method inside a class will be called once when an object of a class is instantiated.
If you use the below code,
static void Main(string[] args)
{
yyy a = new yyy(10);
System.Console.WriteLine(a);
a.ToString();
}
You wil get output as
operator string
string 10
To String