Indexer, Delegates, Anonymous Method, Lambda Expression, Safe & Unsafe code in C#
It is used to store multiple values in one object. It can be called as array on object. If class is created with indexer and when object is created for the class then multiple values can be stored. It contains get and set block. Get block is used to access the value and set block is used to assign the value. Indexer can also be called as "smart array".
Important points about Indexer:
- Indexers are always created with this keyword.
- Indexers are implemented through get and set accessors for the [ ] operator.
- ref and out parameter modifiers are not permitted in indexer.
- Indexers are used on group of elements.
- Indexers are accessed using indexes where as properties are accessed by names.
- Indexer can be overloaded.
1) Example of Indexer having integer values:
2) Example of Indexer having string values:
Delegate is used to copy the reference or address of the method in another variable. It is used to execute methods on events. In windows application, controls will have events. Events are user actions. They are click, double click, key press etc. Delegate is a type or class used to store memory location of the method. Delegate is similar to function pointer in 'c' language.
1) Create a delegate which can be used on a method with two arguments:
To copy the reference of the method in delegate variable then the delegate signature and method signature should be same i.e number of arguments, order of arguments, datatypes should be same. In the given example delegate variable d1 will contain the memory address of the display method.
2) Create the delegate which can be used on the method containing one argument & return type string:
3) Create a delegate which can be used on a method with number of arguments:
Using anonymous method set of statements can be executed while creating variable for the delegate. With this creating class containing method & creating object for the class is not required.
1) Example of Anonymous Method:
2) Create a delegate which can be used on anonymous method, with two arguments:
While working with anonymous method, datatypes, braces, return keyword should be used. With Lambda expression without using all this the result will be displayed.
Example of Lambda Expression:
It is used to execute multiple methods in delegate variables one by one or one after another. The delegate signature & multiple method signature should be same. To copy the method address in delegate variable "+=" is used. To remove method address from delegate variable "-=" is used.
Safe & Unsafe Code:
Both will works with memory location or memory address. Safe code means working with ref & delegates. Unsafe code means working with pointers. Pointers will store or will hold the memory address of the value. In pointers, two symbols are used. i.e (*,&). '*' is used to get the value from memory address. '&' is used to get memory address of the value.
Example of Safe & Unsafe code:
Type this program in notepad & save the file as D:\DotNet>Progeam1.cs. click on program-->visual studio 2012-->Visual Studio Tools-->developer command prompt of VS.net 2012
D:\DotNet>csc /unsafe Program1.cs
In the given example, call by reference is used. i.e while calling or executing the function the reference of the value or memory address of the value will be sent to function creation.
In this way, we have learned Indexer, Delegates, Anonymous Method, Lambda Expression, Safe & Unsafe code in C# with examples in this article. I hope this will help beginners to understand what is Indexer, Delegates, Anonymous Method, Lambda Expression, Safe & Unsafe code and how to implement it.
You may also be interested in...
- Slack Integration with C#
- Xamarin Forms: Getting Started
- Part 1: Introduction to C#
- Part 2: Arrays and Function in C#
- Part 3: OOPs Concepts in C#
- Part 4: Constructor/Destructor in C#
- Part 5: Abstract class in C#
- Part 6: Indexer,Delegates, Anonymous Method, Lambda expression in C#
- Part 7: Collections in C#
- Part 8: Generics in C#
- Part 9: Attributes in C#
- Part 10: Sealed/Partial class in C#
- Part 11: MultiThreading in C#