Object initializer and set initializer in C #3.0

Source: Internet
Author: User

In C #3.0, object initializers and collection initializers are simplified.CodeTo write a line of code that can be written in a few lines. In this way, we will not write a large complex LINQ expression.

I have read several articles about object initializers and collection initializers.ArticleThis is a simple example. Initialization assignment for some special scenarios is not involved, so I will sort out this blog.

Some Questions about object initializers:

Question 1: Can I assign only partial values to the object initiator? That is, do not assign values to some of the values.

Answer: Yes. Refer to the code below.

Question 2: Can the object initiator assign values to internal members? (Private Members do not need to think about it. They certainly cannot assign values .)

Answer: Yes. refer to 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)
{
Person = new person {firstname = "Scott", lastname = "Guthrie", test02 = 56 ,};
Console. writeline (person. test02 );
Console. writeline (person. Age );
Console. Readline ();
}

}

Question 3: Can the object initiator be used together with the constructor?

Answer: You can see the following code for normal use:

VaR cookie3 = new system. net. Cookie ("mycookie", "Jose") {comment = "a cookie "};

In the constructor, we assign a value to the cookie name and value to the comment attribute in the initialization constructor.

Question 4: Which of the following statements is executed first when the constructor assigns a value and the initialization constructor assigns a value?

For example, what is the result of the following code ??

Static void main (string [] ARGs)
{
VaR cookie = new system. net. Cookie ("mycookie", "Jose") {name = "test02", comment = "a cookie "};
Console. writeline (cookie. Name );
Console. Readline ();
}

Answer:

The constructor is executed earlier than the initialization constructor.

The message written by writeline is test02.

Collection initializers:

Question 1: Can a null value be constructed in the Set initialization constructor?

Answer: Yes. See the following code.

Question 2: Can the set initialization constructor initialize hashtable?

Answer: Yes. At this time, two objects are used 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
{< br> static void main (string [] ARGs)
{< br> List People = new list {< br> new person {firstname = "Scott", lastname = "Guthrie ", age = 32 },< br> new person {firstname = "bill", lastname = "Gates", test02 = 85 },< br> new person {firstname = "Susanne ", age = 32 },< br> 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 ();
}
}

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.