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
VulpesRank #55 · 29.2K reputationPosted Jul 24, 2012, 6:12 AM EDT
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.
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jul 24, 2012, 5:09 AM EDT
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
siddharth rai7 reputationPosted Jul 24, 2012, 6:18 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts