One-Step Object Creation and Initialization in C# 3.0, Page 2
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.
0 Comments (click to add your comment)
Networking Solutions
