c#3.0 object initializers and collection initializers

Source: Internet
Author: User
Tags constructor readline

The

C # 3.0 Object initializer (initializers) and the collection initializer (Collection initializers) Simplify our code to write a line of code that would otherwise have been written out. In this way, in the use of LINQ, we do not write a LINQ expression of the huge complexity. Since I've seen several articles about object initializers (objects initializers) and collection initializers (Collection initializers) are a simple example, the initialization assignment of some slightly more specific scenes is not involved, So I'm writing this blog. Some questions about object initializers: Question one: is the object initializer allowing only partial values to be assigned? You don't assign values to some of these values
Answer: allow; Refer to the following code.
Problem Two: The object initializer allows you to assign values to internal members? (The private member must not think, certainly cannot assign value.)
Answer: Allow; Refer to the code below.

public class Person
{
public string FirstName {get; set;}
public string LastName {get; set;}
public int Age {get; set;}
private int test01 = 25;
internal int test02;
} Class Program
{
static void Main (string[] args)
{
person who = new Person {FirstName = "Scott", LastName = "Guthrie", test02 = 56,};
Console.WriteLine (person.test02);
Console.WriteLine (person. Age);
Console.ReadLine ();
Question three: Can the object initializer be used in conjunction with constructors? Answer: You can see the following code can be used normally: var cookie3 = new System.Net.Cookie ("MyCookie", "Jose") {Comment = "a Cookie"}; We assign a value to the name and value of the Cookie in the constructor and assign a value to the Comment property in the initialization constructor. Question four: constructor assignment and initialization constructor assignment that was first executed? such as the following code, the result is that?? static void Main (string[] args)
{
var cookie = new System.Net.Cookie ("MyCookie", "Jose") {Name = "test02", Comment = "A Cookie"};
Console.WriteLine (Cookies. Name);
Console.ReadLine ();
Answer
Constructors are executed earlier than the initialization constructor.
The information written by the above WriteLine is: test02

Some problems with the collection initializer (Collection initializers):

Question one: Is it possible to construct a collection with a null value in the collection initialization builder?
Answer: Yes, refer to the following code.

Question two: Can the collection initialization constructor initialize the hashtable?
Answer: Yes. This is the equivalent of using two objects to initialize the constructor, see the following code:

public class Person
{
public string FirstName {get; set;}
public string LastName {get; set;}
public int Age {get; set;}
private int test01 = 25;
internal int test02;
} Class Program
{
static void Main (string[] args)
{
list<person> people = new list<person>{
New Person {FirstName = "Scott", LastName = "Guthrie", age = 32},
New Person {FirstName = "Bill", LastName = "Gates", test02 = 85},
New Person {FirstName = ' Susanne ', age = 32},
Null
}; Hashtable pp = new Hashtable {
{1, new person {FirstName = ' Scott ', LastName = ' Guthrie ', age = 32}},
{2, new person {FirstName = "Bill", LastName = "Gates", test02 = 85}},
{3, new person {FirstName = ' Susanne ', age = 32}},
{4, null},
}; Console.ReadLine ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.