Exemple #1
0
        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 a System.Int32 to a Square.
            Square sq2 = (Square)90;

            Console.WriteLine("sq2 = {0}", sq2);

            // Converting a Square to a System.Int32.
            int side = (int)sq2;

            Console.WriteLine("Side length of sq2 = {0}", side);

            Console.WriteLine();

            // Implicit cast OK!
            Square s3;

            s3.Length = 7;
            Rectangle rect2 = s3;

            Console.WriteLine("rect2 = {0}", rect2);
            DrawSquare(s3);

            Console.WriteLine();

            // Explicit cast syntax still OK!
            Square s4;

            s4.Length = 3;
            Rectangle rect3 = (Rectangle)s4;

            Console.WriteLine("rect3 = {0}", rect3);
            Console.ReadLine();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Fun with conversions ****");
            //make a rectangle
            Rectangle rect = new Rectangle(15, 4);

            Console.WriteLine(rect.ToString());
            rect.Draw();

            Console.WriteLine();

            //convert r into a square
            Square squ = (Square)rect;

            Console.WriteLine(squ.ToString());
            squ.Draw();
            Console.WriteLine();
            DrawSquare((Square)rect);
            Console.WriteLine("==========Implicit=====");
            Square s3 = new Square(15);

            //Attempt to make implicit cast
            Rectangle rect2 = (Rectangle)s3;

            Console.WriteLine(rect2.ToString());
            rect2.Draw();
            Console.Read();
        }
Exemple #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Conversions *****\n");

            int  a = 123;
            long b = a;  // implicit conversion (no loss of data)

            Console.WriteLine(b);
            int c = (int)b;  // explicit conversion (potential loss of data)

            Console.WriteLine(c);

            // 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 {
                Length = 83
            };

            // Attempt to make an implicit cast?
            Rectangle rect2 = s3;

            Console.WriteLine(rect2);

            Console.ReadLine();
        }
Exemple #4
0
        static void StructConversion()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("=> Convert a rectangle to a square");

            Rectangle r = new Rectangle(15, 4);

            Console.Write($"Rectangle: {r}. "); r.Draw();

            Square s = (Square)r;

            Console.Write($"Square: {s}. "); s.Draw();
        }
Exemple #5
0
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle(15, 4);

            Console.WriteLine(r.ToString());
            r.Draw();

            Square s = (Square)r;

            Console.WriteLine(s.ToString());
            s.Draw();

            Console.Read();
        }
Exemple #6
0
        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 sq2 = (Square)90;

            Console.WriteLine($"sq2 = {sq2}");
            Console.WriteLine();

            int side = (int)sq2;

            Console.WriteLine($"Side length of sq2 = {side}");
            Console.WriteLine();

            Square s3 = new Square {
                Length = 7
            };
            Rectangle rect2 = s3;

            Console.WriteLine($"rect2 = {rect2}");
            Console.WriteLine();

            Square s4 = new Square {
                Length = 3
            };
            Rectangle rect3 = (Rectangle)s4;

            Console.WriteLine($"rect3 = {rect3}");
            Console.WriteLine();

            Console.ReadLine();
        }
Exemple #7
0
        static void Main(string[] args)
        {
            var rectangle = new Rectangle(15, 4);

            rectangle.DoNothing(5);
            Console.WriteLine();
            Console.WriteLine(rectangle.ToString());
            rectangle.Draw();
            Console.WriteLine();
            Square square = (Square)rectangle;

            Console.WriteLine(square.ToString());
            square.Draw();

            Console.ReadLine();
        }
Exemple #8
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Conversions *****\n");
            // Make a Rectangle.
            Rectangle r = new Rectangle(4, 10);

            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();

            // Converting an int to a Square.
            Square sq2 = (Square)90;

            Console.WriteLine("sq2 = {0}", sq2);
            // Converting a Square to an int.
            int side = (int)sq2;

            Console.WriteLine("Side length of sq2 = {0}", side);
            Console.ReadLine();

            // Implicit cast OK!
            Square s3 = new Square {
                Length = 7
            };
            Rectangle rect2 = s3;

            Console.WriteLine("rect2 = {0}", rect2);
            // Explicit cast syntax still OK!
            Square s4 = new Square {
                Length = 3
            };
            Rectangle rect3 = (Rectangle)s4;

            Console.WriteLine("rect3 = {0}", rect3);
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            // Make a Rectangle
            Rectangle r = new Rectangle(15, 4);
            // Convert r into a Square based on the height of the Rectangle.
            Square s = (Square)r;
            // Convert Rectangle to Square to invoke method.
            Rectangle rect = new Rectangle(10, 5);
            // Converting an int to a Square.
            Square sq2 = (Square)90;
            // Converting a Square to an int.
            int side = (int)sq2;
            // Make an implicit cast.
            Square s3 = new Square {
                Length = 7
            };
            Rectangle rect2 = s3;
            // Explicit cast syntax is redundant, but still allowed.
            Square s4 = new Square {
                Length = 3
            };
            Rectangle rect3 = (Rectangle)s4;

            Console.WriteLine("***** Fun with Conversions *****\n");
            Console.WriteLine(r.ToString());
            r.Draw();
            Console.WriteLine();

            Console.WriteLine(s.ToString());
            s.Draw();
            Console.WriteLine();

            DrawSquare((Square)rect);
            Console.WriteLine();

            Console.WriteLine("sq2 = {0}", sq2);
            Console.WriteLine("Side length of sq2 = {0}", side);
            Console.WriteLine("rect2 = {0}", rect2);
            Console.WriteLine("rect3 = {0}", rect3);

            Console.ReadLine();
        }
Exemple #10
0
        public static void Main(string[] args)
        {
            // playing with custom type conversion
            Rectangle rect = new Rectangle(15, 4);

            Console.WriteLine(rect.ToString());
            rect.Draw();

            Console.WriteLine();

            // convert rect to Square... no idea why >.>
            Square s = (Square)rect;

            Console.WriteLine(s.ToString());
            s.Draw();

            Rectangle rect2 = new Rectangle(10, 5);

            DrawSquare((Square)rect2);

            // casting squares and ints
            Square sq2 = (Square)90;

            Console.WriteLine("sq2: {0}", sq2);

            int side = (int)sq2;

            Console.WriteLine("Side length of sq2 is: {0}", side);

            // playing with implicit conversion
            Square s3 = new Square(7);

            s3.Draw();

            Rectangle rect3 = s3;

            rect3.Draw();

            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("******Fun with Conversions******");

            Rectangle r = new Rectangle(15, 4);

            Console.WriteLine(r.ToString());
            r.Draw();

            Console.WriteLine();

            Square s = (Square)r;

            Console.WriteLine(s.ToString());
            s.Draw();


            Rectangle rt = new Rectangle(5, 8);

            DrawSquare((Square)rt);
            //Console.ReadLine();

            int    length = 2;
            Square si     = (Square)length;

            DrawSquare(si);

            Square its = new Square(3);

            Console.WriteLine((int)its);

            Console.WriteLine("********隐式类型转换*********");
            Square    iss       = new Square(2);
            Rectangle rectangle = iss;

            rectangle.Draw();
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("****** Fun with Conversion ******\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.ReadLine();

            Square sq2 = (Square)90;

            Console.WriteLine("sq2 = {0}", sq2);
            int side = (int)sq2;

            Console.WriteLine("Side length of sq2 = {0}", side);

            Square s3 = new Square {
                Lenght = 7
            };
            Rectangle rect2 = s3;

            Console.WriteLine("rect2 = {0}", rect2);

            Square s4 = new Square {
                Lenght = 3
            };
            Rectangle rect3 = (Rectangle)s4;

            Console.WriteLine("rect3 = {0}", rect3);
        }
Exemple #13
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Fun with conversions ****");
            Rectangle r = new Rectangle(14, 5);

            Console.WriteLine(r.ToString());
            r.Draw();

            Square s = (Square)r;

            Console.WriteLine(s.ToString());
            s.Draw();

            DrawSquare((Square)r);

            Square s2 = (Square)90;

            Console.WriteLine("s2 = {0}", s2);
            s2.Draw();

            int side = (int)s2;

            Console.WriteLine("Length of square side: {0}", side);

            Square    s3 = new Square(83);
            Rectangle r2 = s3;

            Console.WriteLine("r2: {0}", r2.ToString());

            Square    s4 = new Square(7);
            Rectangle r3 = (Rectangle)s4;

            Console.WriteLine("r3: {0}", r3);

            Console.ReadLine();
        }
 // This method requires a Square type.
 private static void DrawSquare(Square sq)
 {
     sq.Draw();
 }
Exemple #15
0
 static void DrawSquare(Square sq)
 {
     Console.Write($"Square: {sq}. "); sq.Draw();
 }
 static void DrawSquare(Square sq) {
     Console.WriteLine(sq);
     sq.Draw();
 }
Exemple #17
0
 static void DrawSquare(Square sq)
 {
     Console.WriteLine(sq);
     sq.Draw();
 }
Exemple #18
0
 public static void DrawSquare(Square sq)
 {
     Console.WriteLine(sq.ToString());
     sq.Draw();
 }