-"Linq
1. Implicitly-typed var a=1;var b= "haha"; when used without knowing the type, IL determines the type based on the assignment class and does not affect efficiency. Because IL produces the same in the middle. var must be assigned a value.
2. Anonymous type var entity= new {title= "I am anonymous type", fun= "I am useful"};
Copy the properties of an object to an anonymous object without displaying the name of the specified property, and the original property name is copied to the anonymous object.
Monitoring entity will find that its type is anonymous type (anonymous types)
Do not attempt to access its properties outside the method of creating an anonymous object
Useful for serializing and deserializing JSON on a specific Web site
3. Automatic attributes
4. Object initializer var obj=new obj{id=guid. NewGuid (), title= "haha"}; var arr=list<int> () {1,2,3,4,5};
5. Delegation
6. Generics
Boxing: Value type converted to type object; Unpacking: Object conversion to value type boxing operation can cause performance loss generics solve this problem
Non-generic containers such as the Hashtable Queue stack are boxed, and these containers can only store data of type Object
List<t>, dictionary<tkey,tvalue> are generics
Custom generics
public static Class Somethingfactory<t>
{
public static T InitInstance (t inobj)
{
if (false)
{
return inobj;
}
return default (T);
}
}
Generic constraint: public static class somethingfactory<t> where t:myobj can only pass in MYOBJ types or MYOBJ derived classes
Or write this: the Where t:myobj,new () constraint passed in a type that must have a constructor
Benefits of Generics
<1> Reuse of algorithms
Think about it: the sort algorithm for list types is useful for all types of list collections
<2> type safety
<3> Improve Performance
No type conversions, guaranteed type safety on the one hand, and improved performance on the other
<4> Better readability
7. Generic delegate
The delegate is basically classified as Class 3:
<1>predicate generic Delegate
var d1=new predicate<int> (more);
Definition: Represents a method that defines a set of conditions and determines whether a specified object conforms to those conditions
Obj object to compare by the criteria defined in the method represented by this delegate
T the type of object to compare
Returns the result true if obj conforms to the condition defined in the method represented by this delegate; False otherwise
Pubic delegate bool Predicate<in t> (T obj);
Disadvantage definition too dead, must have a return value, must have a parameter
<2>action generic Delegate
can have 0 to 16 input parameters, the type of input parameter is indeterminate, but cannot have a return value
var d3 = new Action (noparamnoreturnaction);
var d4 = new Action<int, string> (twoparamnoreturnaction);
Note: The int and string in angle brackets are the input parameters of the method
static void Noparamnoreturnaction ()
{
Do-You want
}
static void Twoparamnoreturnaction (int A, string b)
{
Do-You want
}
<3>func generic Delegate
compensate for the action generic delegate, cannot return the value of the insufficient, the specified to have a return value, the type of the return value is also determined by the consumer
var d5=new func<int,string> (Oneparamonereturnfunc);
Note: The string type (the last generic type) is the return value type of the method
static string Oneparamonereturnfunc (int a)
{
Do-You want
return string. Empty;
}
8. Anonymous Methods
var arr = new List<int> () {1, 2, 3, 4, 5, 6, 7, 8};
var D1 = new Moreorlessdelgate (more);
var D1 = new predicate<int> (more);
var D1 = new Predicate<int> (delegate (int item)
{
Can access variables in the current context
Console.WriteLine (arr. Count);
if (item > 3)
{
return true;
}
return false;
});
Print (arr, D1);
Console.WriteLine ("OK");
<1> Better Code readability
<2> can access variables in the current context
9. Extension methods
Extension methods are static methods in static classes
public static Class MyClass
{
public static void Extstring (this String val)
{
Console.WriteLine (Val);
}
}
var a= "AAA";
A.extstring ();
----AAA----
<1> extension methods must be defined in a non-nested, non-generic static class
The <2> extension method must be a static method
<3> extension methods must have at least one parameter
<4> The first parameter must be appended with the This keyword as a prefix
<5> The first parameter cannot have other modifiers (such as ref or out)
<6> The first parameter cannot be a pointer type
Precautions
<1> as with several of the preceding features, the extension method only increases the compiler's work and does not affect performance (adding attributes to a type in an inherited way can affect performance)
<2> If there is a method in the original class that is the same as your extension method (at least it is the same), then your extension method award will not be called and the compiler will not prompt you
<3> extension methods are too powerful to affect architecture, patterns, readability, etc...
MVC Series Essay II