c# 2.0 點滴

來源:互聯網
上載者:User

1.) value types cannot be assigned null because, by definition, they can't contain references,To declare variables that can store null you use the nullable modifier, ?. int? count = null; Assigning null to value types is especially attractive in database programming.

2.)In C# 2.0, all the numeric primitive types include a static tryParse() method.similar to the Parse() method,but if fails, the tryParse() method returns false not a exception

3.)In C# 2.0, it is possible to use the default() operator to determine the default value of a data type,such as default(int) return 0

4.)In C# 2.0, System.Array.Resize equal vb's Redim

5.)Static class introduced in C# 2.0, public static class name

6.) const:
  Just as with const local variables, a const field contains a compile-time-determined value that cannot be changed at runtime.
    readonly:
  the readonly modifier is available only for fields (not for local variables) and it declares that the field value is modifiable only from inside the constructor or directly during declaration.
  Unlike constant fields, readonly fields can vary from one instance to the next. In fact, a readonly field's value can change from its value during declaration to a new value within the constructor. Furthermore, readonly fields occur as either instance or static fields.
  Another key distinction is that you can assign the value of a readonly field at execution time rather than just at compile time.
  Using readonly with an array does not freeze the contents of the array. It freezes the number of elements in the array because it is not possible to reassign the readonly field to a new instance.

7.)Properties:
 In C# 2.0, support was added for placing an access modifier on either the get or the set portion of theim property plementation (not on both), By using private on the setter, the property appears as read-only to classes other than class which the property belong to

 Properties and Method Calls Not rAllowed as ef or out Parameter Values,because there are no memory address to pass

8.)Iterators:
public class Bruteforce<T> : IEnumerable<T>{
 public System.Collections.IEnumerator<T> GetEnumerator(){
  ...
  yield return item;
  ...
 }

 //multi iterators within a class (when foreach in ....GetReverseEnumerator())
 public IEnumerable<T> GetReverseEnumerator()              
  {
   yield return Second;
   yield return First;
  }
}
Canceling Further Iteration: yield break;similar to placing a return statement at the top of a function

9.)2.0,delegate such as GreaterThanHandler,instance as GreaterThan,pass as param you can directly use GreaterThan,
prior to 2.0 you should new GreaterThanHandler(GreaterThan)

10.)Anonymous Methods:
GreaterThanHandler greaterThan;
greaterThan =                                                     
           delegate(int first, int second)                              
           {                                                            
               return (first < second);                                 
           }; 
BubbleSort(items, greaterThan);
or
BubbleSort(items,                                                     
         delegate(int first, int second)                                   
         {                                                                 
             return (first < second);                                      
         }                                                                 
      );
you cannot use the typeof() operator on an anonymous method, and calling GetType() is possible only after assigning the anonymous method to a delegate variable.
Variables that programmers declare outside an anonymous method and access within the implementation are outer variables.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.