Anonymous Methods

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 which provide a technique to pass a code block as a delegate parameter. Anonymous methods are basically methods without a name, just the body. An anonymous method is inline unnamed method in the code. It is created using the delegate keyword and doesn’t required name and return type. Hence we can say an anonymous method has only body without name, optional parameters and return type. An anonymous method behaves like a regular method and allows us to write inline code in place of explicitly named methods. To test this, add a new class AnonymousMethod.cs and write the following code


class AnonymousMethod {
	static void Main() {
		SayDel sd = delegate(string name) {
			return "Hello Mr. " + name + " have a nice day.";
		}
		;
		Console.WriteLine(sd("hello"));
		Console.WriteLine(sd("Scanftree"));
		Console.WriteLine(sd("hii"));
		Console.ReadLine();
	}
}


Quantitative Aptitude
Reasoning
Programming
Interview