Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand object initializer and collection initializer in C # 3.0

Shulou Source: shulou.com Published: 2022-06-02 09:16:22 09月30日 Update

This article mainly explains "how to understand object initializers and set initializers in C # 3.0". The explanation in this article is simple and clear and easy to learn and understand. let's study and learn how to understand object initializers and set initializers in C# 3.0.

Some questions about object initializers (Object Initializers):

Question 1: does the object initializer allow only partial values to be assigned? That is, some of these values are not assigned.

Answer: allow; refer to the following code.

Question 2: does the object initializer allow you to assign values to members of internal? (private members certainly don't have to think about it, and they certainly can't assign values. )

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 person = new Person {FirstName = "Scott", LastName = "Guthrie", test02 = 56,}; Console.WriteLine (person.test02); Console.WriteLine (person.Age); Console.ReadLine ();}}

Question 3: can object initializers be used in conjunction with constructors?

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

Var cookie3 = new System.Net.Cookie ("MyCookie", "Jose") {Comment = "a cookie"}

We assign a value to the name and value of Cookie in the constructor and a value to the Comment property in the initialization constructor.

Question 4: which constructor assignment and initialization constructor assignment is performed?

For example, the following code, which one is the result?

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 the above WriteLine is: test02

Some problems with set initializers (Collection Initializers):

Question 1: can an item in the collection initialization constructor be null?

Answer: yes, see the following code.

Question 2: can the collection initialization constructor initialize Hashtable?

Answer: yes. This is equivalent to initializing the constructor with two objects, 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 people = new List {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 () }} Thank you for your reading, the above is the content of "how to understand object initializers and set initializers in C# 3.0". After the study of this article, I believe you have a deeper understanding of how to understand object initializers and collection initializers in C# 3.0, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Objects questions code answers constructors functions learning content members references affirmations no two information names that is attributes ideas situations articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi MySQL Docker Shulou Technology