Key Points for Anonymous Method in C#
There are some key points to keep in mind whenever you are implementing the anonymous method in C#.
- Inline delegate is another name for this technique.
- This technique allows you to build a delegate object without writing many methods.
- This method has access to the outer method's variables. Outer variables are a subset of such variables.
Examples
Let us see a few code examples related to Anonymous Method in C#.
Code 1
using System;
public delegate void Show(string x);
class AnonymousMethodDemo{
public static void identity(Show mycode, string coder)
{
coder = " Coding" + coder;
mycode(coder);
}
static public void Main()
{
identity(delegate(string coder) {
Console.WriteLine("Coding Ninjas"+
" is one of the best institutions for {0}", coder); }, " and Learning.");
}
}
Output
Coding Ninjas is one of the best institutions for Coding and Learning.
In this example, we have created an anonymous function and printed some output.
Code 2
using System;
delegate void NumberChanger(int n);
namespace DelegateAppl {
class TestDelegateDemo {
static int num = 10;
public static void AddNum(int p) {
num += p;
Console.WriteLine("Named Method: {0}", num);
}
public static void MultNum(int q) {
num *= q;
Console.WriteLine("Named Method: {0}", num);
}
public static int getNum() {
return num;
}
static void Main(string[] args) {
NumberChanger nc = delegate(int x) {
Console.WriteLine("Anonymous Method: {0}", x);
};
nc(100);
nc = new NumberChanger(AddNum);
nc(15);
nc = new NumberChanger(MultNum);
nc(20);
Console.ReadKey();
}
}
}
Output
Anonymous Method: 100
Named Method: 25
Named Method: 500
In this example, we have used the anonymous method. Here delegate calls both named and anonymous methods, in the same way, using the delegate object to pass the method parameters.
Frequently Asked Questions
What are the advantages of using Anonymous Methods in C#?
Anonymous methods in C# are less challenging to type. When there is a little code, anonymous methods are usually recommended.
What are the limitations of Anonymous Methods in C#?
A jump statement like goto, break, or cannot be used in an anonymous method in C#. An outer method's ref or out argument cannot be accessed. The anonymous methods cannot access or have access to the dangerous code.
How to use the Anonymous Method in C#?
We build a method with the same signature as the Predicate Generic Delegate. Then we build a Predicate Generic Delegate instance.
Then we send the Predicate Instance as a parameter to the List collection class's Find function.
Conclusion
In this article, we have covered the Anonymous Method in C#. We have introduced the topic along with why we need this method. We also discussed its key points. We have addressed the code implementation along with their output and explanation.
Enhance your skills in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and more with our Coding Ninjas Studio Guided Path. If you want to sharpen your coding skills to the test, check out the mock test series and enter the contests on Coding Ninjas Studio! If you are just a beginner and want to know what questions big giants like Amazon, Microsoft, and Uber ask, check the problems, interview experiences, and interview bundle for placement preparations.
We hope that this article might have helped you in enhancing your knowledge. If you liked this article, please give it a thumbs up!
“Happy Coding”!.