O'Reilly logo

Visual C#® 2012: How to Program, Fifth Edition by Harvey Deitel, Paul Deitel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

10.13. Object Initializers

Visual C# provides object initializers that allow you to create an object and initialize its public properties (and public instance variables, if any) in the same statement. This can be useful when a class does not provide an appropriate constructor to meet your needs, but does provide properties that you can use to manipulate the class’s data. The following statements demonstrate object initializers using the class Time2 from Fig. 10.5.

// create a Time2 object and initialize its properties Time2 aTime = new Time2 { Hour = 14, Minute = 30, Second = 12 }; // create a Time2 object and initialize only its Minute property Time2 anotherTime = new Time2 { Minute = 45 };

The first statement ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required