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.