Two Valuable Features in C#4.0

來源:互聯網
上載者:User
文章目錄
  • C# has used duck typing for a long time

1. Duck Typing ("If it walks like a duck and quacks like a duck, it must be a duck.") [Refer to here]

Duck typing allows an object to be passed in to a method that expects a certain type even if it doesn’t inherit from that type. All it has to do is support the methods and properties of the expected type in use by the method.
my object does not have to support all methods and properties of duck to be passed into a method that expects a duck. Same goes for a method that expects a rabbit. It only needs to support the methods and properties of the expected type that are actually called by the method.

 Example:

 

  1. public interface IReadOnlyRestricable   
  2.   {   
  3.   bool ReadOnly { get; set; }   
  4.   } 

然後我們要遍曆所有的控制項,找出有ReadOnly屬性的控制項並把此屬性設為true(譯者註:這些控制項本身沒有實現 IReadOnlyRestricable),在ducktyping下我們可以把控制項通過類型轉換為IReadOnlyRestricable,就像下 面代碼一樣,這樣我們就不需要通過反射去定位ReadOnly屬性了:

 

 

  1. foreach (Control c in f.Controls)   
  2.   {   
  3.   //希望有隱式轉換  
  4. IReadOnlyRestrictable if interface contract is in class we are checking against   
  5.   IReadOnlyRestricable editable = c as IReadOnlyRestricable;   
  6.   if (editable != null)   
  7.      editable.ReadOnly = true;   
  8.   }

C# has used duck typing for a long time

Interestingly enough, certain features of C# already use duck typing. For example, to allow an object to be enumerated via the C# foreach operator, the object only needs to implement a set of methods as Krzystof Cwalina of Microsoft points out in this post...

Provide a public method GetEnumerator that takes no parameters and returns a type that has two members: a) a method MoveNext that takes no parameters and return a Boolean, and b) a property Current with a getter that returns an Object.

You don’t have to implement an interface to make your object enumerable via the foreach operator.

 

 

From Bruce eckel:

What I’m trying to get to is that in my experience there’s a balance between the value of strong static typing and the resulting impact that it makes on your productivity. The argument that "strong static is obviously better" is generally made by folks who haven’t had the experience of being dramatically more productive in an alternative language. When you have this experience, you see that the overhead of strong static typing isn’t always beneficial, because sometimes it slows you down enough that it ends up having a big impact on productivity. 

 

2. Safe Null checking

  1. //這將返回null,如果客戶是空或者如果命令是空    
  2. int? orderNumber = Customer?.Order?.OrderNumber;

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.