I've just found strange for me code in Jeffrey Richter book (CLR via C# 4.0, page 257) and have misunderstanding why it works so.
public sealed class Classroom
{
private List<String> m_students = new List<String>();
public List<String> Students { get { return m_students; } }
public Classroom() { }
}
class Program
{
static void Main(string[] args)
{
Classroom classroom = new Classroom {
Students = { "Jeff", "Kristin" }
};
foreach (var student in classroom.Students)
Console.WriteLine(student);
}
}
Result:
Jeff
Kristin
As you can see, we have an accessor property named 'Students', which has only getter (not setter!), but in 'Main' function, when we'd like to initialize 'classroom' variable, we initializing 'Students' field of 'Classroom' type:
Classroom classroom = new Classroom {
Students = { "Jeff", "Kristin" }
};
I always thought, that when a variable in 'left-side' of expression (int i = 1), then compiler should access to setter function, and when in 'right-side' (int x = i + 2) to getter function.
Why in Jeffrey's code so interesting behavior (may be it's just for me? sorry if it's so).
From section 7.6.10.2 of the C# 5 spec:
A member initializer that specifies a collection initializer after the equals sign is an initialization of an embedded collection. Instead of assigning a new collection to the field or property, the elements given in the initializer are added to the collection referenced by the field or property. The field or property must be of a collection type that satisfies the requirements specified in §7.6.10.3.
So this code:
Classroom classroom = new Classroom {
Students = { "Jeff", "Kristin" }
};
is equivalent to:
Classroom tmp = new Classroom();
tmp.Students.Add("Jeff");
tmp.Students.Add("Kristin");
Classroom classroom = tmp;
Basically, =
within an object initializer isn't exactly the same as a standalone assignment statement.
EDIT: This code
Classroom classroom = new Classroom {
Students = new List<string> { "Jeff", "Kristin" }
};
would fail to compile, as that would try to call a setter for Student
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With