In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.