// Declare a delegate.
Delegate VoidPrinter (StringS );
ClassTestclass
{
Static VoidMain ()
{
// Instatiate the delegate type using an anonymous method.
Printer P =Delegate(StringJ)
{
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 =NewPrinter (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 VoidDowork (StringK)
{
System. Console. writeline (k );
}
}
/* Output:
The delegate using the anonymous method is called.
The delegate using the named method is called.
*/
Anonymous object
In C #3.0, we do not need to waste time on these irrelevant types. By using the "anonymous type", you only need to use a new expression without a type name when you need such an object.VaRB1=New{Name="The first sample book",Price=88. 0f };
-
- VaRB2=New{Price=25. 0f,Name="The second sample book"};
- VaRB3=New{Name="The third sample book",Price=35. 00f };
-
-
-
- Console. writeline (b1.gettype ());
-
- Console. writeline (b2.gettype ());
-
- Console. writeline (b3.gettype ());