Feel the elegance of C #3.0

Source: Internet
Author: User
Q: Is it really necessary to use C #3.0?
A: No, but we cannot ignore its advantages.

-------- Here is an example I gave --------------------------

Only when encoding is started can you understand the elegance of C #3.0.

What do you feel about the same type of effect written in C #2.0 and 3.0 respectively?CodeIt becomes more concise. Without the repetitive work between attributes and their associated fields, you can also write less "boring" constructor methods. Many may have reserved opinions on syntax simplification (including myself), but it is undeniable that 3.0 of the syntax is more conducive to maintenance. When our bloated project code is reduced from 50000 lines to 20000 lines, therefore, it brings more than just a few mouse clicks on the keyboard.

C #2.0 Class User
{
Private   String Name;
Private   Int Age;
Private List < String > Interest;

Public User ()
{
Interest= NewList<String>();
Interest. Add ("Reading");
Interest. Add ("Internet access");
}

Public User ( String Name, Int Age ): This ()
{
This. Name=Name;
This. Age=Age;
}

Public   String Name
{
Get   {ReturnName ;}
Set   {Name=Value ;}
}
Public   Int Age
{
Get   {ReturnAge ;}
Set   {Age=Value ;}
}

Public List < String > Interest
{
Get {ReturnInterest ;}
}
}

// Invoke
User user =   New User ( " Tom " , 21 );

C #3.0 Class User
{
Public User ()
{
Interest =   New List < String >   {"Reading","Internet access"} ;
}

Public   String Name {Get;Set;}
Public   Int Age {Get;Set;}
Public List < String > Interest {Get;Private Set;}
}

// Invoke
VaR user =   New User
{
Name= "Tom",
Age= 21,
} ;

In my project, there are also mixins such as charextension, stringextension, and objectextension. As for Lambda, anonymous types, and LINQ, needless to say.

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.