Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Anonymous classes, anonymous methods
Static type, cannot be called directly because static type determines that there is no Id attribute (c# 3.0). Dynamic, dynamic compilation, instance is read-only, can avoid compiler check (c# 4.0). Anonymous type () generates a generic anonymous class at compile time, and the attributes in the class are read-only. 4. Anonymous methods are syntactic sugar, and there is actually a complete method after compilation by the compiler. 5. Var is a syntactic sugar that is used by the compiler to infer the actual type from the type on the right (used with anonymous types, complex types).Copy the code
Object model = new {Id = 2,}; // dynamic dModel = new {Id = 2,}; Var varModel = new {Id = 2,}; Action1 = new Action(delegate () {console.writeline (" anonymous method "); });Copy the code
Second, the extension method
1. Definition of extension method:
A new feature introduced in C#3.0, most commonly in LINQ, extension methods are a special type of static method that can provide new members to instances of the type without changing the source code for that type.Copy the code
2. Steps to build the extension method:
1. Create a static class. Create a static method in the class. 2. Add at least one parameter to the static method, preceded by the this keyword, which tells the compiler that the current method is an extension method. This method will become a new member of the type of the first parameter.Copy the code
// <summary> // // </summary> public static class ExtendMethod {// </summary> // </summary> // </ param name="list"></param> /// <returns></returns> public static List<string> extendInstance(this List<string> list) { return list; }}Copy the code
1. Attention should be paid to the use of extended methods:
1, if the extended method and the same instance method is subject to the instance method, a large number of use needs to pay attention to. 2. If the permission of this modifier is too large, its subclasses can use the whole method, such as extending the method parameter to object, which ultimately affects the readability of the code. 3. Do not extend base types.Copy the code
Linq to Object
Inheriting from the IEnumable interface, all operations are already in memory data, can be said to be very fast.Copy the code
Linq to object
1. Simple implementation of lambda's little chestnut
Public static list <int> Where(this list <int> list, Func<int, bool> predicate) { List<int> result = new List<int>(); foreach (var item in list) { if (predicate.Invoke(item)) { result.Add(item); } } return list; }} a List < int > List = new List < int > (),2,3,4,5,6,7,8 {1}; Var result = list.Where(t=>t>3); // output 4,5,6,7,8Copy the code
2. Implement a lambda after adding IEnumerable and Yield
Yield: is a state machine, which can be understood as simply recording the entire operation and not calculating it in time, implementing delayed fetching and querying only when needed.Copy the code
Public static IEnumerable<T> Where<T>(this IEnumerable<T> tList, Func<T, bool> predicate) { if (tList == null || predicate == null) { throw new Exception("can't be null"); } foreach (var item in tList) { if (predicate.Invoke(item)) { yield return item; }}} a List < int > List = new List < int > (),2,3,4,5,6,7,8 {1}; // Since the yield return call does not calculate the result, ToList can convert it if you want to get the result directly. var result = list.Where(t=>t>3); Foreach (var I in result){}Copy the code
Linq to Object
A List < int > List = new List < int > (),2,3,4,5,6,7,8 {1}; Var list = from s in list where s>3 select s; Var list = from list where s>3 select new{a = s+1, b = s+2}; var list = from list where s>3 select new{a = s+1, b = s+2};Copy the code