Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
 How to: Initialize Objects without ...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
C# Language Reference
How to: Initialize Objects without Calling a Constructor (C# Programming Guide)

You can use object initializers to initialize type objects in a declarative manner without having to invoke the type's constructor.

The following examples show how to use object initializers with named objects. Anonymous types must be declared with an object initializer. For more information, see How to: Return Subsets of Element Properties in a Query (C# Programming Guide).

The following example shows how to initialize a single new StudentName type by using an object initializer.

C#
StudentName student = new StudentName
{
    FirstName = "Craig",
    LastName = "Playstead",
    ID = 116
};

The following example shows how to initialize a collection of StudentName types by using a collection initializer. Note that a collection initializer is a series of comma-separated object initializers.

C#
List<StudentName> students = new List<StudentName>()
{
  new StudentName {FirstName="Craig", LastName="Playstead", ID=116},
  new StudentName {FirstName="Shu", LastName="Ito", ID=112},
  new StudentName {FirstName="Gretchen", LastName="Rivas", ID=113},
  new StudentName {FirstName="Rajesh", LastName="Rotti", ID=114}
};

To run this code, copy and paste the class into a Visual C# console application project that has been created in Visual Studio. By default, this project targets version 3.5 of the .NET Framework, and it has a reference to System.Core.dll and a using directive for System.Linq. If one or more of these requirements are missing from the project, you can add them manually. For more information, see How to: Create a LINQ Project.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Misleading information...      MEta_COg   |   Edit   |   Show History
This does not prevent the need to call a constructor. The default constructor of the type will still be called. That means this will not work if the type has a private parameterless constructor, as the default constructor will not be accessible. This will also not help avoid situations in which a new instance is needed of a type that is unknown until runtime. There is a need for a way to create a new instance of a type, without calling any constructor, as the types default constructor may not be accessible, or it may perform actions that are undesired.

public class TestClass1 {
public TestClass1() {
Console.WriteLine("TestClass1 constructor called.");
}}

public class TestClass2 {
private TestClass2() {
Console.WriteLine("TestClass2 constructor called.");
}}

//////////////////

static void Main() {
TestClass1 test1 = new TestClass1 { }; // Output: TestClass1 constructor called.
TestClass2 test2 = new TestClass2 { }; // Compile error, TestClass2.TestClass2()' is inaccessible due to its protection level
}




Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker