C # I three times daily

Try to avoid boxing and unboxing few:

string A = "A " + 0 ;
 string B = "A " + 0 .ToString (); // efficient
 // the ToString () int is to be done directly by operating the memory string conversion efficiency is much higher than the packing

  ArrayList and List <T>, as far as possible without using the List ArrayList, ArrayList would be packing unpacking operation, the value stored in memory is converted into Object (packing), converted to a corresponding value type (unpacking); List <T > will not

StringBuilder string operations using large

The difference between const and readonly to use

Const high efficiency, compile-time constants, that is, the natural static, static keyword can not be added to the preceding modifications. Const reference to the actual value will be used where the variable corresponding const after the compiler compiles code in place. Only modifies yl cell type, enumerated type or string type

readonly efficiency can, run-time constant, for the first time after the assignment can not be changed, there is no limit readonly modifications are members of a class instance, to make him a member of the class, you need to add static in front, so that you can directly use the class name call. (constructor to initialize variables can be assigned)

Operator overloading

class Test 
{ 
    public  int a;
    public  static test operator + (Test has, Test b) 
    { 
      a.par + = b.par;
      return a; 
    } 
}

== and Equals

Equals for equality comparison reference type, == equality comparison value for the type of 

If the two variables are used to compare the values ​​contained equal, then it is defined as "a value equal";

If the comparison of two variables reference the same memory, then it is defined as "reference equality."

dynamic and var (dynamic not get to know)

dynamic is a new feature of FrameWork4.0. The emergence of dynamic C # has a weak type of language features. The compiler at compile time is no longer on the type of check, compile default dynamic object supports any features you want. For example, even if the object you are returned to GetDynamicObject method ignorant, as you can call the code as follows, the compiler does not complain:

dynamic dynamicObject = GetDynamicObject();
Console.WriteLine(dynamicObject.Name);
Console.WriteLine(dynamicObject.SampleMethod());

In fact, var and dynamic entirely different concept, simply should not be placed together for comparison. var actually compile thrown our "syntactic sugar", once compiled, the compiler will automatically match the actual type of the variable var, and type to replace the actual declare the variable, it looks as if we coding time It is declared with the actual type. And the dynamic is compiled, is actually a type of object, but the compiler will type dynamic special treatment, it does not do any type checking at compile time, but the type checking into a run.

Add or delete operation does not support the foreach loop

If the increase in the foreach / delete an element, an exception is thrown.

The reason: foreach loop uses an iterator to traverse the collection, it maintained a control version of the collection inside the iterator FCL provided.

Collection Version: is simply an integer variable, any set of additions and deletions operation will cause the +1 version number.

foreach loop calls MoveNext method to facilitate elements inside the MoveNext method will detect the version number, the version number in case of changes, will throw an exception.

The object is no longer used for reference when necessary, the assignment is null

delegate与event

public  delegate  void ActionCall (); 

public  Event ActionCall Call; 

// delegate can be assigned (=) and binding (+ = - =), the event can only be bound (+ = - =) 

// event is called in this class, other classes can not be called;

 Expansion method: does not change the original classes, add methods to the class

public  class the Test 
{ 
    public  void the Action () 
    { 
    } 
} 
// extension method must be static class, the extension method must be static 
public  static  class TestExtension 
{ 
    public  static  void ActionExtension ( the this the Test T) // first parameter must be to the type of extension, and this must be added the keyword 
    { 
    }
   public static void ActionExtension1(this Test t, int a)
    {
    }
} 
// This time you can call a method to expand
the Test t = new new the Test ();
t.ActionExtension ();
t.
ActionExtension1 (0);
 
   

Note: does not support extended attributes and events, but can be extended Interface

dynamic is a new feature of FrameWork4.0. The emergence of dynamic C # has a weak type of language features. The compiler at compile time is no longer on the type of check, compile default dynamic object supports any features you want. For example, even if the object you are returned to GetDynamicObject method ignorant, as you can call the code as follows, the compiler does not complain:

dynamic dynamicObject = GetDynamicObject();
Console.WriteLine(dynamicObject.Name);
Console.WriteLine(dynamicObject.SampleMethod());

In fact, var and dynamic entirely different concept, simply should not be placed together for comparison. var actually compile thrown our "syntactic sugar", once compiled, the compiler will automatically match the actual type of the variable var, and type to replace the actual declare the variable, it looks as if we coding time It is declared with the actual type. And the dynamic is compiled, is actually a type of object, but the compiler will type dynamic special treatment, it does not do any type checking at compile time, but the type checking into a run.

Add or delete operation does not support the foreach loop

If the increase in the foreach / delete an element, an exception is thrown.

The reason: foreach loop uses an iterator to traverse the collection, it maintained a control version of the collection inside the iterator FCL provided.

Collection Version: is simply an integer variable, any set of additions and deletions operation will cause the +1 version number.

foreach loop calls MoveNext method to facilitate elements inside the MoveNext method will detect the version number, the version number in case of changes, will throw an exception.

The object is no longer used for reference when necessary, the assignment is null

delegate与event

public  delegate  void ActionCall (); 

public  Event ActionCall Call; 

// delegate can be assigned (=) and binding (+ = - =), the event can only be bound (+ = - =) 

// event is called in this class, other classes can not be called;

 Expansion method: does not change the original classes, add methods to the class

public  class the Test 
{ 
    public  void the Action () 
    { 
    } 
} 
// extension method must be static class, the extension method must be static 
public  static  class TestExtension 
{ 
    public  static  void ActionExtension ( the this the Test T) // first parameter must be to the type of extension, and this must be added the keyword 
    { 
    }
   public static void ActionExtension1(this Test t, int a)
    {
    }
} 
// This time you can call a method to expand
the Test t = new new the Test ();
t.ActionExtension ();
t.
ActionExtension1 (0);
 
  

Note: does not support extended attributes and events, but can be extended Interface

dynamic is a new feature of FrameWork4.0. The emergence of dynamic C # has a weak type of language features. The compiler at compile time is no longer on the type of check, compile default dynamic object supports any features you want. For example, even if the object you are returned to GetDynamicObject method ignorant, as you can call the code as follows, the compiler does not complain:

dynamic dynamicObject = GetDynamicObject();
Console.WriteLine(dynamicObject.Name);
Console.WriteLine(dynamicObject.SampleMethod());

In fact, var and dynamic entirely different concept, simply should not be placed together for comparison. var actually compile thrown our "syntactic sugar", once compiled, the compiler will automatically match the actual type of the variable var, and type to replace the actual declare the variable, it looks as if we coding time It is declared with the actual type. And the dynamic is compiled, is actually a type of object, but the compiler will type dynamic special treatment, it does not do any type checking at compile time, but the type checking into a run.

Add or delete operation does not support the foreach loop

If the increase in the foreach / delete an element, an exception is thrown.

The reason: foreach loop uses an iterator to traverse the collection, it maintained a control version of the collection inside the iterator FCL provided.

Collection Version: is simply an integer variable, any set of additions and deletions operation will cause the +1 version number.

foreach loop calls MoveNext method to facilitate elements inside the MoveNext method will detect the version number, the version number in case of changes, will throw an exception.

The object is no longer used for reference when necessary, the assignment is null

delegate与event

public  delegate  void ActionCall (); 

public  Event ActionCall Call; 

// delegate can be assigned (=) and binding (+ = - =), the event can only be bound (+ = - =) 

// event is called in this class, other classes can not be called;

 Expansion method: does not change the original classes, add methods to the class

public  class the Test 
{ 
    public  void the Action () 
    { 
    } 
} 
// extension method must be static class, the extension method must be static 
public  static  class TestExtension 
{ 
    public  static  void ActionExtension ( the this the Test T) // first parameter must be to the type of extension, and this must be added the keyword 
    { 
    }
   public static void ActionExtension1(this Test t, int a)
    {
    }
} 
// This time you can call a method to expand
the Test t = new new the Test ();
t.ActionExtension ();
t.
ActionExtension1 (0);
 
 

Note: does not support extended attributes and events, but can be extended Interface

Guess you like

Origin www.cnblogs.com/xianguoguo/p/11097278.html