匿名メソッドと匿名オブジェクト


//Declare a delegate.delegate void Printer(string s);class TestClass{    static void Main()    {       //Instatiate the delegate type using an anonymous method.        Printer p = delegate(string j)        {            System.Console.WriteLine(j);        };       //Results from the anonymous delegate call.        p("The delegate using the anonymous method is called.");       //The delegate instantiation using a named method "DoWork".        p = new Printer(TestClass.DoWork);       //Results from the old style delegate call.        p("The delegate using the named method is called.");    }   //The method associated with the named delegate.    static void DoWork(string k)    {        System.Console.WriteLine(k);    }}/* Output:    The delegate using the anonymous method is called.    The delegate using the named method is called.*/
 
匿名オブジェクト
 
C#3.0では、これらの重要でないタイプのために時間を浪費する必要はありません.「匿名タイプ」を使用すると、このようなオブジェクトが必要な場合にタイプ名のないnew式を使用します.var b1 = new { Name = "The First Sample Book", Price = 88.0f };  

 
   
  1. var b2 = new { Price = 25.0f, Name = "The Second Sample Book" };  
  2. var b3 = new { Name = "The Third Sample Book"Price = 35.00f };  
  3.  
  4. Console.WriteLine(b1.GetType());  
  5. Console.WriteLine(b2.GetType());  
  6. Console.WriteLine(b3.GetType());