Geeks With Blogs
Bruce Ge November 2009 Entries
Type constraints sadness
Although delegate type and Enum type are class types, we can not inherit from them as they are sealed classes after compile, public delegate void DelegateTestType(); //Compile time error class myTest : DelegateTestType { ... } we can not make it as a type constraint either on class or method: //Compile time error class myTest<T> where T : DelegateTestType { } From the C# 2.0 specification we can read (20.7, Constraints): A class-type constraint must satisfy the following rules: ยท The type must ......

Posted On Saturday, November 21, 2009 10:17 PM

Delegate in anonymous type
We all know the usage of the anonymous type: var obj = new { Name = "John", Age = 24 }; What if we want to add a anonymous delegate or Lamba to it? Like: var obj = new { Name = "John", Age = 24, behaviour=delegate(string name, int age) { return "Customer Name: " + name + ", Age: " + age; } }; or var obj = new { Name = "John", Age = 24, behaviour = (name, age) => string.Format( "Hello, {0}! You are {1} years old.", name, age ) }; The above two will cause compile time error, because anonymous type ......

Posted On Tuesday, November 10, 2009 1:42 PM

Copyright © argot | Powered by: GeeksWithBlogs.net