October 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

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.

 

 


Sitemap | Contact Us

Rocket Fuel