.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Code Snippets >> Generics >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Calling Anonymous Methods

Posted By:Mahesh       Posted Date: March 29, 2011    Points: 40    Category: Generics    URL: http://www.dotnetspark.com  

Following is the code which allows us to call Anonymous Methods using delegates

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// 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)

        // 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)


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend