There are problems occurred while understanding type casting in C#. I created a simple class and overloaded the ToString() method for it, so that the values of the class object fields are output in a string:
public class Triple{
public int Int32;
public string String;
public bool Boolean;
public Triple(int Int32, string String, bool Boolean)
{
this.Int32 = Int32;
this.String = String;
this.Boolean = Boolean;
}
public override string ToString()
{
return String.Format("{0},{1},{2}", this.Int32, this.String, this.Boolean);
}
I also set an implicit conversion of an object of the Triple class to the bool type:
public static implicit operator bool(Triple T1)
{
if (T1.Boolean)
{
return true;
}
else
{
return false;
}
}
Now when I call:
Triple t1 = new Triple(1, "abcd", true);
Console.WriteLine(t1);
The Boolean field of the Triple class is shows as output, not the value of the class fields.
Why is this happening?