Anonymous methods and anonymous objects

Source: Internet
Author: User

// 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 };

 
 
  1. VaRB2=New{Price=25. 0f,Name="The second sample book"};
  2. VaRB3=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 ());

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.