dcsimg
August 15, 2018
Hot Topics:

One-Step Object Creation and Initialization in C# 3.0

  • May 10, 2006
  • By Vipul Patel
  • Send Email »
  • More Articles »

You can also have embedded initialization in ScienceClass, but you will need to declare your ScienceClass a little differently, as shown below:

public class ScienceClass
   {
      Student s1 = new Student();
      Student s2 = new Student();
      Student s3 = new Student();
      public Student Student1
      {
         get
         {
            return s1;
         }
         set
         {
            s1 = value;
         }
      }
      public Student Student2
      {
         get
         {
            return s2;
         }
         set
         {
            s2 = value;
         }
      }
      public Student Student3
      {
         get
         {
            return s3;
         }
         set
         {
      s3 = value;
         }
      }
   }

One-Step Object Creation and Initialization

Object initializers provide a new syntax to initialize the objects you create. This simple syntax combines object creation and initialization into a single step.

Download the Code

You download the source code for this article here.

About the Author

Vipul Patel is a Microsoft MVP (two years in a row) in Visual C# and currently works at Microsoft through Volt Information Sciences. He specializes in C# and deployment issues. You can reach him at Vipul_d_patel@hotmail.com.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

Sitemap

Thanks for your registration, follow us on our social networks to keep up-to-date