Delegate, Anonymous Method and Lamda Expression in C# (Part 2/2)
What are anonymous method? Anonymous methods are similar to normal methods but they do not have a name and can be directly assigned to a delegate. These methods are not reusable components that is why they don’t have a method name. They make the code simple and more readable. One reason why we need an anonymous method is that often times a method is created so that it can be assigned to the delegate instance and called through the delegate reference. How to create an anonymous method? Anonymous methods are methods without a name which are directly created and assigned to a delegate instance. See code below where displayTime is an instance of delegate Display that accepts no parameter and has voice return type. Display displayTime = delegate (){ Console .WriteLine( DateTime .Now.ToString( "dd/mm/yyyy" )); }; delegate () mea ns that this anonymous method takes no parameter and {} defines the block of statements to be run when thi...