What is this called in C#?
Book x = 开发者_运维知识库new Book(){
Author = "Rowling",
Title = "Harry Potter",
Genre = Genres.Fantasy
};
I've tried googling for Inline declaration, but I guess thats not what it's officially called.
Thoughts?
From what I've heard, it's called an object initializer.
Look here: http://msdn.microsoft.com/en-us/library/bb384062.aspx
This in itself appears to be illegal code. It's very close to an object initializer but that would require the member being set to be paired with the value it was set to. For example
Book x = new Book() {
Author = "Rowling",
Title = "Harry Potter",
Category = Geners.Fantasy };
EDIT OP corrected the question to now have the correct object initializer syntax.
Object Initializers
Have alook at Object and Collection Initializers (C# Programming Guide)
精彩评论