Example #1
0
        static void Main( string[] args )
        {
            Console.WriteLine("***** Fun with Object Init Syntax *****\n");

            // Make a Point by setting each property manually.
            Point firstPoint = new Point();
            firstPoint.X = 10;
            firstPoint.Y = 10;
            firstPoint.DisplayStats();

            // Or make a Point via a custom constructor.
            Point anotherPoint = new Point(20, 20);
            anotherPoint.DisplayStats();

            // Or make a Point using object init syntax.
            Point finalPoint = new Point { X = 30, Y = 30 };
            finalPoint.DisplayStats();

            // Calling a more interesting custom constructor with init syntax.
            Point goldPoint = new Point(PointColor.Gold) { X = 90, Y = 20 };
            goldPoint.DisplayStats();

            // Create and initialize a Rectangle.
            Rectangle myRect = new Rectangle
            {
                TopLeft = new Point { X = 10, Y = 10 },
                BottomRight = new Point { X = 200, Y = 200 }
            };
            myRect.DisplayStats();

            Console.ReadLine();
        }
Example #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Fun with Object Init Syntax *****");

            Point firstPoint = new Point();
            firstPoint.X = 10;
            firstPoint.Y = 10;
            firstPoint.DisplayStats();

            Point anotherPoint = new Point(20, 20);
            anotherPoint.DisplayStats();

            Point finalPoint = new Point { X = 30, Y = 30 };
            finalPoint.DisplayStats();

            Point goldPoint = new Point(PointColor.Gold) { X = 90, Y = 20 };
            goldPoint.DisplayStats();

            Rectangle myRect = new Rectangle
            {
                TopLeft = new Point { X = 10, Y = 10 },
                BottomRight = new Point { X = 200, Y = 200 }
            };

            myRect.DisplayStats();

            Console.ReadLine();
        }
Example #3
0
    static void Main(string[] args)
    {
      // The old way of initializing a Student's properties.
      Student nicholas = new Student();
      nicholas.Name = "Nicholas";
      nicholas.Address = "809087 Some Lost Street, Las Vegas, NM 89000";
      nicholas.GradePointAverage = 3.51;
      PrintStudentInfo(nicholas);

      // The new object initializer syntax: new X { P1 = a, P2 = b, ... };
      Student randal = new Student
      { // Property names with initializers.
        Name = "Randal",  
        Address = "123 Elm Street, Truth Or Consequences, NM 00000",
        GradePointAverage = 3.51
      };
      PrintStudentInfo(randal);

      // Another simple example of the new syntax.
      LatitudeLongitude latLong = new LatitudeLongitude() { Latitude = 35.3, Longitude = 104.9 };
      Console.WriteLine("Latitude {0}, Longitude {1}", latLong.Latitude, latLong.Longitude);

      // A more complex class whose properties are themselves class objects.
      // Here we use the new syntax for Rectangle and for its two Point properties.
      Rectangle rect = new Rectangle
      {
        UpperLeftCorner = new Point { X = 3, Y = 4 },
        LowerRightCorner = new Point { X = 5, Y = 6 }
      };
      Console.WriteLine("Rectangle with upper left corner at ({0},{1}) and "
        + "lower right corner at ({2},{3})", rect.UpperLeftCorner.X, rect.UpperLeftCorner.Y,
        rect.LowerRightCorner.X, rect.LowerRightCorner.Y);

      // Wait for user to acknowledge results.
      Console.WriteLine("Press Enter to terminate...");
      Console.Read();
    }
Example #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Fun with obj init syntax *****\n");
            //make a point by setting each prop manually.
            Point firstPoint = new Point();
            firstPoint.X = 10;
            firstPoint.Y = 10;
            firstPoint.DisplayStats();

            //or make a point via a custom ctor
            Point anotherPoint = new Point(20, 20);
            anotherPoint.DisplayStats();

            //or make a point using object init syntax
            Point finalPoint = new Point { X = 30, Y = 30 };
            finalPoint.DisplayStats();

            //calling a bit interesting custom ctor w init syntax
            Point goldPoint = new Point(PointColor.Gold) { X = 90, Y = 20 };
            goldPoint.DisplayStats();

            Rectangle myRect = new Rectangle {
                TopLeft = new Point { X = 10,Y=10 },
                BottomRight = new Point{X=200, Y=200}
            };
            //the old-school method for the Rectangle above...
            //Rectangle r = new Rectangle();
            //p1.X=10;
            //p1.Y=10;
            //r.TopLeft = p1;
            //Point p2 = new Point();
            //p2.X=200;
            //p2.Y=200;
            //r.BottomRight = p2;
            Console.ReadLine();
        }