https://www.youtube.com/watch?v=drgsIO5M8ok
See our other step by step video series below.
Learn MVC 5 in 16 hours:- https://goo.gl/dmdakg
Learn AngularJS 1.5 Step by Step in 8 hours :- https://goo.gl/F3ovSr
Learn Angular 2.0 Step by Step in 8 hours:- http://tinyurl.com/z3vnvhg
Learn Design Pattern in 8 hours:- https://goo.gl/eJdn0m
Learn C# in 100 hours :- https://goo.gl/FNlqn3
Learn MSBI in 32 hours:- https://goo.gl/TTpFZN
Learn SharePoint Step by Step in 8 hours:- https://goo.gl/XQKHeP
Learn TypeScript in 45 Minutes :- https://goo.gl/oRkawI
In this .NET and C# video we will see a small sample for Anonymous methods and in what situations to use anonymous methods.
For more such videos visit http://www.questpond.com
We are also distributing a 100 page Ebook ".NET Interview Questions". If you want this ebook please share this video in your facebook/twitter/linkedin account and email us on questpond@questpond.com with the shared link and we will email you the PDF.
https://msdn.microsoft.com/en-us/library/0yw3tz5k.aspx
Remarks
See our other step by step video series below.
Learn MVC 5 in 16 hours:- https://goo.gl/dmdakg
Learn AngularJS 1.5 Step by Step in 8 hours :- https://goo.gl/F3ovSr
Learn Angular 2.0 Step by Step in 8 hours:- http://tinyurl.com/z3vnvhg
Learn Design Pattern in 8 hours:- https://goo.gl/eJdn0m
Learn C# in 100 hours :- https://goo.gl/FNlqn3
Learn MSBI in 32 hours:- https://goo.gl/TTpFZN
Learn SharePoint Step by Step in 8 hours:- https://goo.gl/XQKHeP
Learn TypeScript in 45 Minutes :- https://goo.gl/oRkawI
In this .NET and C# video we will see a small sample for Anonymous methods and in what situations to use anonymous methods.
For more such videos visit http://www.questpond.com
We are also distributing a 100 page Ebook ".NET Interview Questions". If you want this ebook please share this video in your facebook/twitter/linkedin account and email us on questpond@questpond.com with the shared link and we will email you the PDF.
https://msdn.microsoft.com/en-us/library/0yw3tz5k.aspx
Anonymous Methods (C# Programming Guide)
Visual Studio 2015
In versions of C# before 2.0, the only way to declare a delegate was to use named methods. C# 2.0 introduced anonymous methods and in C# 3.0 and later, lambda expressions supersede anonymous methods as the preferred way to write inline code. However, the information about anonymous methods in this topic also applies to lambda expressions. There is one case in which an anonymous method provides functionality not found in lambda expressions. Anonymous methods enable you to omit the parameter list. This means that an anonymous method can be converted to delegates with a variety of signatures. This is not possible with lambda expressions. For more information specifically about lambda expressions, see Lambda Expressions (C# Programming Guide).
Creating anonymous methods is essentially a way to pass a code block as a delegate parameter. Here are two examples:
// Create a handler for a click event. button1.Click += delegate(System.Object o, System.EventArgs e) { System.Windows.Forms.MessageBox.Show("Click!"); };
// Create a delegate. delegate void Del(int x); // Instantiate the delegate using an anonymous method. Del d = delegate(int k) { /* ... */ };
By using anonymous methods, you reduce the coding overhead in instantiating delegates because you do not have to create a separate method.
For example, specifying a code block instead of a delegate can be useful in a situation when having to create a method might seem an unnecessary overhead. A good example would be when you start a new thread. This class creates a thread and also contains the code that the thread executes without creating an additional method for the delegate.
void StartThread() { System.Threading.Thread t1 = new System.Threading.Thread (delegate() { System.Console.Write("Hello, "); System.Console.WriteLine("World!"); }); t1.Start(); }
Remarks
The scope of the parameters of an anonymous method is the anonymous-method-block.
It is an error to have a jump statement, such as goto, break, or continue, inside the anonymous method block if the target is outside the block. It is also an error to have a jump statement, such as goto, break, or continue, outside the anonymous method block if the target is inside the block.
The local variables and parameters whose scope contains an anonymous method declaration are called outer variables of the anonymous method. For example, in the following code segment, n is an outer variable:
int n = 0; Del d = delegate() { System.Console.WriteLine("Copy #:{0}", ++n); };
A reference to the outer variable n is said to be captured when the delegate is created. Unlike local variables, the lifetime of a captured variable extends until the delegates that reference the anonymous methods are eligible for garbage collection.
No unsafe code can be accessed within the anonymous-method-block.
Anonymous methods are not allowed on the left side of the is operator.
Example
The following example demonstrates two ways of instantiating a delegate:
- Associating the delegate with an anonymous method.
- Associating the delegate with a named method (DoWork).
In each case, a message is displayed when the delegate is invoked.
// Declare a delegate. delegate void Printer(string s); class TestClass { static void Main() { // Instantiate 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. */
No comments:
Post a Comment