Indexers in C-Sharp

Hello friends, in this article we are going to learn indexers in c#.

We can called indexers as smart arrays in C#. We can store instances of a class like an array. We can use class and structure for indexers. The indexed value can be set or retrieved without explicitly specifying a type or instance member.

Indexer are defined in pretty much same way as properties.

The following example defines two classes first for Student and other for StudentIndexer.

using System; namespace ConsoleApp1 { public class Student { public int StudentId { get; set; } public string StudentName { get; set; } } public class StudentIndexer { private Student[] cnt = new Student[5]; public Student this[int i] { get { return cnt[i]; } set { cnt[i] = value; } } } }

We can store Student class instances as follows.

StudentIndexer s = new StudentIndexer(); s[0] = new Student() { StudentId = 1, StudentName = "Jhonson" }; s[1] = new Student() { StudentId = 2, StudentName = "Raydu" }; s[2] = new Student() { StudentId = 3, StudentName = "William" }; s[3] = new Student() { StudentId = 4, StudentName = "jack" }; s[4] = new Student() { StudentId = 5, StudentName = "kate" };

We can access Student instance with in Indexers as follows.

for(int i=0; i<5;i++) { Console.WriteLine($"Student Id: {s[i].StudentId} Student Name: {s[i].StudentName}"); }

Entire final Student Indexer program will be.

using System; namespace ConsoleApp1 { public class Student { public int StudentId { get; set; } public string StudentName { get; set; } } public class StudentIndexer { private Student[] cnt = new Student[5]; public Student this[int i] { get { return cnt[i]; } set { cnt[i] = value; } } } class program1 { public static void Main() { StudentIndexer s = new StudentIndexer(); s[0] = new Student() { StudentId = 1, StudentName = "Jhonson" }; s[1] = new Student() { StudentId = 2, StudentName = "Raydu" }; s[2] = new Student() { StudentId = 3, StudentName = "William" }; s[3] = new Student() { StudentId = 4, StudentName = "jack" }; s[4] = new Student() { StudentId = 5, StudentName = "kate" }; for(int i=0; i<5;i++) { Console.WriteLine($"Student Id: {s[i].StudentId} Student Name: {s[i].StudentName}"); } Console.ReadKey(); } } }

Important Points:

1) Indexers enable objects to be indexed like an arrays.

2) A get accessor returns a value. A set accessor assigns a value.

3) Indexers can have more than one formal parameter, for example, when accessing a two-dimensional array.

4) Indexers are always created with this keyword.

5) Parameterized property are called indexer.

6) Indexer can be overloaded.

7) Indexers do not have to be indexed by an integer value. it is up to you how to define.

8) ref and out parameter modifiers are not permitted in indexer.

9) Indexer is an instance member so can't be static but property can be static.

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories