オブジェクト初期化子を使用すると、型のコンストラクターを明示的に呼び出すことなく、宣言型の方法で型オブジェクトを初期化できます。
次の例は、名前付きオブジェクトでオブジェクト初期化子を使用する方法を示しています。 コンパイラは、最初にパラメーターなしのインスタンス コンストラクターにアクセスし、次にメンバーの初期化を処理することで、オブジェクト初期化子を処理します。 そのため、パラメーターなしのコンストラクターがクラスで private
として宣言されている場合、パブリック アクセスを必要とするオブジェクト初期化子は失敗します。
匿名型を定義する場合は、オブジェクト初期化子を使用する必要があります。 詳細については、「 クエリで要素プロパティのサブセットを返す方法」を参照してください。
例
次の例は、オブジェクト初期化子を使用して新しい StudentName
型を初期化する方法を示しています。 次の使用例は、StudentName
型のプロパティを設定します。
public class HowToObjectInitializers
{
public static void Main()
{
// Declare a StudentName by using the constructor that has two parameters.
StudentName student1 = new StudentName("Craig", "Playstead");
// Make the same declaration by using an object initializer and sending
// arguments for the first and last names. The parameterless constructor is
// invoked in processing this declaration, not the constructor that has
// two parameters.
StudentName student2 = new StudentName
{
FirstName = "Craig",
LastName = "Playstead"
};
// Declare a StudentName by using an object initializer and sending
// an argument for only the ID property. No corresponding constructor is
// necessary. Only the parameterless constructor is used to process object
// initializers.
StudentName student3 = new StudentName
{
ID = 183
};
// Declare a StudentName by using an object initializer and sending
// arguments for all three properties. No corresponding constructor is
// defined in the class.
StudentName student4 = new StudentName
{
FirstName = "Craig",
LastName = "Playstead",
ID = 116
};
Console.WriteLine(student1.ToString());
Console.WriteLine(student2.ToString());
Console.WriteLine(student3.ToString());
Console.WriteLine(student4.ToString());
}
// Output:
// Craig 0
// Craig 0
// 183
// Craig 116
public class StudentName
{
// This constructor has no parameters. The parameterless constructor
// is invoked in the processing of object initializers.
// You can test this by changing the access modifier from public to
// private. The declarations in Main that use object initializers will
// fail.
public StudentName() { }
// The following constructor has parameters for two of the three
// properties.
public StudentName(string first, string last)
{
FirstName = first;
LastName = last;
}
// Properties.
public string? FirstName { get; set; }
public string? LastName { get; set; }
public int ID { get; set; }
public override string ToString() => FirstName + " " + ID;
}
}
オブジェクト初期化子を使用して、オブジェクトにインデクサーを設定できます。 次の例では、インデクサーを使用して異なる位置でプレイヤーを取得および設定する BaseballTeam
クラスを定義します。 初期化子は、位置の省略形または各ポジションの野球スコアカードに使用される数に基づいて、プレイヤーを割り当てることができます。
public class HowToIndexInitializer
{
public class BaseballTeam
{
private string[] players = new string[9];
private readonly List<string> positionAbbreviations = new List<string>
{
"P", "C", "1B", "2B", "3B", "SS", "LF", "CF", "RF"
};
public string this[int position]
{
// Baseball positions are 1 - 9.
get { return players[position-1]; }
set { players[position-1] = value; }
}
public string this[string position]
{
get { return players[positionAbbreviations.IndexOf(position)]; }
set { players[positionAbbreviations.IndexOf(position)] = value; }
}
}
public static void Main()
{
var team = new BaseballTeam
{
["RF"] = "Mookie Betts",
[4] = "Jose Altuve",
["CF"] = "Mike Trout"
};
Console.WriteLine(team["2B"]);
}
}
次の例は、パラメーターの有無に関係なくコンストラクターを使用してコンストラクターとメンバーの初期化を実行する順序を示しています。
public class ObjectInitializersExecutionOrder
{
public static void Main()
{
new Person { FirstName = "Paisley", LastName = "Smith", City = "Dallas" };
new Dog(2) { Name = "Mike" };
}
public class Dog
{
private int age;
private string name;
public Dog(int age)
{
Console.WriteLine("Hello from Dog's non-parameterless constructor");
this.age = age;
}
public required string Name
{
get { return name; }
set
{
Console.WriteLine("Hello from setter of Dog's required property 'Name'");
name = value;
}
}
}
public class Person
{
private string firstName;
private string lastName;
private string city;
public Person()
{
Console.WriteLine("Hello from Person's parameterless constructor");
}
public required string FirstName
{
get { return firstName; }
set
{
Console.WriteLine("Hello from setter of Person's required property 'FirstName'");
firstName = value;
}
}
public string LastName
{
get { return lastName; }
init
{
Console.WriteLine("Hello from setter of Person's init property 'LastName'");
lastName = value;
}
}
public string City
{
get { return city; }
set
{
Console.WriteLine("Hello from setter of Person's property 'City'");
city = value;
}
}
}
// Output:
// Hello from Person's parameterless constructor
// Hello from setter of Person's required property 'FirstName'
// Hello from setter of Person's init property 'LastName'
// Hello from setter of Person's property 'City'
// Hello from Dog's non-parameterless constructor
// Hello from setter of Dog's required property 'Name'
}
new
キーワードのないオブジェクト初期化子
new
キーワードを指定せずにオブジェクト初期化子構文を使用して、入れ子になったオブジェクトのプロパティを初期化することもできます。 この構文は、読み取り専用プロパティで特に便利です。
public class ObjectInitializerWithoutNew
{
public class Address
{
public string Street { get; set; } = "";
public string City { get; set; } = "";
public string State { get; set; } = "";
}
public class Person
{
public string Name { get; set; } = "";
public Address HomeAddress { get; set; } = new(); // Property with setter
}
public static void Examples()
{
// Example 1: Using object initializer WITHOUT 'new' keyword
// This modifies the existing Address instance created in the constructor
var person1 = new Person
{
Name = "Alice",
HomeAddress = { Street = "123 Main St", City = "Anytown", State = "CA" }
};
// Example 2: Using object initializer WITH 'new' keyword
// This creates a completely new Address instance
var person2 = new Person
{
Name = "Bob",
HomeAddress = new Address { Street = "456 Oak Ave", City = "Somewhere", State = "NY" }
};
// Both approaches work, but they behave differently:
// - person1.HomeAddress is the same instance that was created in Person's constructor
// - person2.HomeAddress is a new instance, replacing the one from the constructor
Console.WriteLine($"Person 1: {person1.Name} at {person1.HomeAddress.Street}, {person1.HomeAddress.City}, {person1.HomeAddress.State}");
Console.WriteLine($"Person 2: {person2.Name} at {person2.HomeAddress.Street}, {person2.HomeAddress.City}, {person2.HomeAddress.State}");
}
}
この方法では、新しいオブジェクトを作成するのではなく、入れ子になったオブジェクトの既存のインスタンスを変更します。 詳細と例については、「 クラス型のプロパティを持つオブジェクト初期化子」を参照してください。
こちらも参照ください
.NET