static void Main( string[] args ) { Console.WriteLine("***** Fun with Conversions *****\n"); // Make a Rectangle. Rectangle r = new Rectangle(15, 4); Console.WriteLine(r.ToString()); r.Draw(); Console.WriteLine(); // Convert r into a Square, // based on the height of the Rectangle. Square s = (Square)r; Console.WriteLine(s.ToString()); s.Draw(); Console.WriteLine(); // Convert Rectangle to Square to invoke method. Rectangle rect = new Rectangle(10, 5); DrawSquare((Square)rect); Console.WriteLine(); // Converting an int to a Square. Square sq2 = (Square)90; Console.WriteLine("sq2 = {0}", sq2); // Converting a Square to a int. int side = (int)sq2; Console.WriteLine("Side length of sq2 = {0}", side); Console.WriteLine(); Square s3 = new Square(); s3.Length = 83; // Attempt to make an implicit cast? Rectangle rect2 = s3; Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Conversions *****\n"); Rectangle r = new Rectangle(15, 4); Console.WriteLine(r.ToString()); r.Draw(); Console.WriteLine(); Square s = (Square) r; Console.WriteLine(s.ToString()); s.Draw(); Console.WriteLine(); Rectangle rect = new Rectangle(10, 5); DrawSquare((Square) rect); Console.WriteLine(); Square sqs = (Square) 90; Console.WriteLine("sq2 = {0}\n", sqs); int side = (int) sqs; Console.WriteLine("Side length of sqs = {0}\n", side); Square s3 = new Square(); s3.Length = 7; Rectangle rect2 = s3; Console.WriteLine("rect2 = {0}", rect2); DrawSquare(s3); Console.WriteLine(); Square s4 = new Square(); s4.Length = 3; Rectangle rect3 = (Rectangle) s4; Console.WriteLine("rect3 = {0}", rect3); Console.ReadLine(); }
static void DrawSquare(Square sq) { Console.WriteLine(sq); sq.Draw(); }
// This method requires a Square type. static void DrawSquare(Square sq) { Console.WriteLine(sq.ToString()); sq.Draw(); }
// This method requires a Square type. private static void DrawSquare(Square sq) { sq.Draw(); }
static void DrawSquare(Square sq) { Console.Write($"Square: {sq}. "); sq.Draw(); }