This program is given in the following website (http://www.c-sharpcorner.com/uploadfile/prvn_131971/polymorphism-in-C-Sharp/). Is there any simple example program which can demonstrate the use of IFormattable like in this example? Problem is highlighted.
// notice the comments/warnings in the Main
// calling overridden methods from the base class
using System;
class TestClass
{
public class Square
{
public double x;
// Constructor:
public Square(double x)
{
this.x = x;
}
public virtual double Area()
{
return x * x;
}
}
class Cube : Square
{
// Constructor:
public Cube(double x) : base(x)
{
}
// Calling the Area base method:
public override double Area()
{
return (6 * (base.Area()));
}
}
public static void MyFormat(IFormattable value, string formatString)
{
Console.WriteLine("{0}\t{1}", formatString, value.ToString(formatString, null));
}
public static void Main()
{
double x = 5.2;
Square s = new Square(x);
Square c = new Cube(x); // This is OK, polymorphic
// a base reference can refer to the derived object
Console.Write("Area of Square = ");
MyFormat(s.Area(), "n");
Console.Write("Area of Cube = ");
MyFormat(c.Area(), "n");
// ERROR: Cube q = new Square(x);
// Cannot implicitly convert type 'TestClass.Square' to 'TestClass.Cube'
Cube q1 = (Cube)c; // This is OK, polymorphic
Console.Write("Area of Cube again = ");
MyFormat(q1.Area(), "n");
Console.ReadLine();
// try uncommenting the following line and see if it works
// Cube q2 = (Cube) new Square(x);
// This compiles but is this OK? NO!
//Runtime Exception occurs here: System.InvalidCastException:
//An exception of type System.InvalidCastException was thrown.
// A derived reference should not be transformed from base object,
// since the orphaned parts may occur in the derived.
// Cube constructors those may be necessary had not worked up to now for q2.
// C# compiles code but gives an exception during runtime
}
}
Loading
Posted Sep 17, 2012, 1:19 PM
VulpesPosted Sep 17, 2012, 8:46 AM
However, you can create variables or parameters of an interface type and such variables can be assigned references to objects of any 'concrete' type which implements the interface.
So here we have a parameter of type IFormattable which can be assigned anything which implements IFormattable. It's therefore a way of writing generic code and is allowed by the compiler because it 'knows' that such objects must implement the members of the interface.
Notice that since an interface is a reference type, when you assign value types to it (such as int or DateTime) the latter have to be 'boxed' first. The mechanism is just the same as when assigning value types to variables of the 'object' type.
Posted Sep 17, 2012, 7:58 AM
VulpesPosted Sep 17, 2012, 4:36 AM
It's just a generic method for printing values using a specific format to the console.
Here's a very simple example of its use:
using System;
class Test
{
static void Main()
{
double d = 13;
MyFormat(d, "f2"); // print with 2 decimal places
DateTime dt = DateTime.Now;
MyFormat(dt, "dd-MM-yy"); // print with day first, hyphen separated, 2 digit year
Console.ReadKey();
}
public static void MyFormat(IFormattable value, string formatString)
{
Console.WriteLine("{0}\t{1}", formatString, value.ToString(formatString, null));
}
}