Have questions about computing? Enlighten them on the Forum in this.
Benefits of subscribing to the RSS feed New Features C # 3.0 - Lambda Expressions
Mar 11

Technorati marks:

image Until the C # 2.0, the only way to declare delegates was using normal methods, with an assigned name, which was declared as separate methods.

The anonymous methods are essentially a way to pass a code block as a parameter, using a delegate.

The use of anonymous methods can reduce the code, does it need to create a separate method, so the code is clean and intuitive.

The specification of a block of code as delegate can be very useful in situations where a method is unnecessary. For example the following code, which launched a new thread:

  void StartThread () 
  ( 
      System.Threading.Thread t1 = new System.Threading.Thread 
        (delegate () 
        ( 
            System.Console.Write ( "Hello"); 
            System.Console.WriteLine ( "World!"); 
        )); 
      t1.Start (); 
  ) 

Following is the code of a class, we can see the difference between a delegate associated with a normal (as it was before the C # 2.0), and using an anonymous method (C # 2.0).

  using System; 

  namespace ConsoleApplication 
  ( 
      / / Declare the Delegate 
      internal delegate void Printer (string s); 

      class Program 
      ( 
          static void Main (string [] args) 
          ( 
              / / Instatiate the delegate type using an anonymous method: 
              Printer p = delegate (string j) 
              ( 
                  Console.WriteLine (j); 
              ); 

              / / Results from the anonymous delegate call: 
              p ( "The delegate using the anonymous method is called."); 

              / / The delegate instantiation using a method named "DoWork" 
              p = new Printer (Program.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) 
          ( 
              Console.WriteLine (k); 
          ) 
      ) 
  ) 

As you can see the previous code, the use of anonymous methods, not adequate to declare separate methods to associate the delegate, just declare them once the instantiation of it.

The anonymous methods are just an easier way of writing the code, because after compiled, the method is generated separately, as was the case in version 1.1. The view of the code generated is all the same, we only work for the compiler. :)

rss_icon_glass_reflection64

If you liked this article subscribe to the RSS feed istomesmo to stay updated on the latest articles.

»Subscribe here fedde RSS '

Learn more about RSS fedde here


Articles that may interest you:

published by NC Tags:


One Response to "C # 2.0 - What methods are anonymous?"

  1. 1st John Oliveira says:

    Another feature is very good q ...

    ... But that only complicates the reuse of code and best practice Object Oriented Programming, Design Patterns in particular! :-S

Leave a Reply