More and more humane. Net C#,vb. NET language Features: Automatic properties, object initializers, and collection initializers

Source: Internet
Author: User

--------------------------------------------------------------------------------
Please keep the following information when quoting or reprint:
Dashan [Msn:a3news (at) hotmail.com]
Http://www.zpxp.com http://www.brawdraw.com
Online graphic image processing of radish mouse
--------------------------------------------------------------------------------

1. Automatic attributes (Automatic properties)
believe that C # developers have encountered the following tedious get/set:
(Repetitive mechanical Labor!) I was so upset about it that I simply installed a plugin called Vsproperty in vs.
public class Person
{

private string _truename;
private string _nickname;
private int _age;

public string Truename
{
get {
return _truename;
}
set {
_truename= value;
}
}

public string Nickname
{
get {
return _nickname;
}
set {
_nickname= value;
}
}

public int Age
{

get {
return _age;
}
set {
_age = value;
}
}
}

Thankfully, there's been a change in VS2008, and you can do this:
public class Person
{
public string Truename {get; set; }
public string Nickname {get; set; }
public int Age {get; set; }
}

2.object Initializer (initializers):

Previous initialization mode:
person who = new person ();
Person. Truename = "Johson";
Person. Nickname = "Big can Mountain";
Person. Age = 30;

Now you can:
person person = new Person {truename= "Johnson", nickname = "Dashan", age=30}; It's convenient to have a row!

How do I add a mailing address?
Can be changed to:
person who = new person
{
Truename = "Johnson",
Nickname = "Big can Mountain"
Age = 30,
Address = new Address {
Street = "Shenzhen Press Group No. No. 6008 Futian District Shennan Avenue",
City = "Shenzhen",
Province = "Guangdong province",
Zip = 518009
}
};
Note that address is also a new addition, direct new!

3. Collection Initializers (Collection initializers)
You can use this:
list<person> people = new list<person> ();

People. ADD (new person {truename = "Johnson", nickname = "Dashan", age = 30});
People. ADD (new person {truename = "Bill", nickname = "Brother Bill", age = 40});
People. ADD (new person {truename = "Jim", nickname = "Small Liang elder brother", age = 20});

You can even do this:
list<person> people = new List<person> {
New Person {truename = "Johnson", nickname = "Dashan", age = 30},
New Person {truename = "Bill", nickname = "Brother Bill", age = 40},
New Person {truename = "Jim", nickname = "Little Liang elder brother", age = 20}
};
(and a few add)

In a word, more and more humane!

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.