In recent years, the C # language has been a long way, the code style is more concise and beautiful, such as common generics and its constraints, nullable types, implicit types, anonymous types and delegates, the following table can be used in this part of the relatively simple characteristics of the use of a preliminary understanding.
Characteristics |
Example |
Generics and their constraints |
Class sample<t> where T:stream |
Nullable types |
Nullable<int>,int? Note that this nullable type is a struct rather than a class |
Implicitly-typed |
var i = 0; |
Anonymous type |
var Xionger = new {name= "Xionger", age=27} |
simplified iterator |
{yield return i;} |
Commissioned |
Action<t>, Func<t, Tresult> the concept of closures (similar to JavaScript) should be observed when using delegates to understand the life cycle of a closure object |
Extension methods |
public static bool IsNull (this String x) {} |
Dynamic type |
Dynamic expando = new ExpandoObject (), which is useful in making some template methods and reusing code. |
Partial class |
Partial class, usage scenario: Classes that extend the code generation tool, some service classes are too large, logically split, and easy to maintain |
Lambda expressions and expression trees |
Returnlength = text + text.length, whereas an expression tree is a tree structure similar to a syntax tree. Expression Firstarg = expression.constant (2); Expression Secondarg = expression.constant (3); Expression add = Expression.add (Firstarg, Secondarg); |
Since this part of the feature understanding and use is relatively easy to describe, and then the expression Tree, LINQ and asynchronous model is relatively complex, will be described in detail.
LINQ can be used to call two different data models in a uniform way through an intermediate model, which is very handy, followed by a simple table that describes the common operations of LINQ, and in practice, the way in which an individual is compared to the recommended extension method.
Operations and Extensions |
Example |
Select element |
var query = from user in sampledata.allusers Select User; |
filtering and sorting |
var query = from defect in sampledata.alldefects Span style= "font-family: Song body; font-size:14px; " >where defect. Status! = status.closed where defect. AssignedTo = Tim orderby defect. Severity descending select defect. Summary; |
let words |
introduce local variables let length = User. Name.length orderby length select new {Name = user. Name, length = length} |
< P> connect |
Describes a relatively complex grouping connection. var query = from defect in sampledata.alldefects join subscription in Sampledata.allsubscriptions on defect. Project equals subscription. Project into groupedsubscriptions select new {Defect = Defect, Subscription = Groupedsubscriptions } |
Group |
var query = from defect in sampledata.alldefects Group defect by defect. AssignedTo |
In addition, LINQ provides a very simple API approach to SQL, XML, and object collections to view the relevant blog http://www.cnblogs.com/luckdv/articles/1728088.html.
This is very interesting to give TPL the c#5 feature, which translates the invocation of an asynchronous operation into a synchronous form, which is very handy, and a very simple example is shown below.
1 Async voidDisplaywebsitelength (Objectsender, EventArgs e)2 3 {4 5Label.text ="fetching ...";6 7 using(HttpClient client =NewHttpClient ()) {8 9task<string> task = client. Getstringasync ("www.bjork.com");Ten OneString Text =awaittask; A -Label.text =text. Length.tostring (); - the}}
View Code
Resources
- ( British ) Jon, skeet. "In-depth understanding C # ( version 3 ) " [M]. Beijing : People's post and Telecommunications press ,.
Quick Start Series--in-depth understanding C #