Home Backend Development C#.Net Tutorial [C# Tutorial] C# Indexer (Indexer)

[C# Tutorial] C# Indexer (Indexer)

Dec 24, 2016 pm 01:21 PM
c#

C# Indexer

Indexer allows an object to be indexed like an array. When you define an indexer for a class, the class behaves like a virtual array. You can access instances of this class using the array access operator ([ ]).

Syntax

The syntax of a one-dimensional indexer is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

element-type this[int index]

{

   // get 访问器

   get

   {

      // 返回 index 指定的值

   }

 

   // set 访问器

   set

   {

      // 设置 index 指定的值

   }

}

Copy after login

Purpose of Indexer

The declaration of the behavior of an indexer is similar to a property to some extent. Just like properties, you can define indexers using the get and set accessors. However, properties return or set a specific data member, while indexers return or set a specific value of an object instance. In other words, it breaks the instance data into smaller parts and indexes each part, getting or setting each part.

Defining a property includes providing the property name. The indexer is defined without a name, but with the this keyword, which points to the object instance. The following example demonstrates this concept:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

using System;

namespace IndexerApplication

{

   class IndexedNames

   {

      private string[] namelist = new string[size];

      static public int size = 10;

      public IndexedNames()

      {

         for (int i = 0; i < size; i++)

         namelist[i] = "N. A.";

      }

      public string this[int index]

      {

         get

         {

            string tmp;

 

            if( index >= 0 && index <= size-1 )

            {

               tmp = namelist[index];

            }

            else

            {

               tmp = "";

            }

 

            return ( tmp );

         }

         set

         {

            if( index >= 0 && index <= size-1 )

            {

               namelist[index] = value;

            }

         }

      }

 

      static void Main(string[] args)

      {

         IndexedNames names = new IndexedNames();

         names[0] = "Zara";

         names[1] = "Riz";

         names[2] = "Nuha";

         names[3] = "Asif";

         names[4] = "Davinder";

         names[5] = "Sunil";

         names[6] = "Rubic";

         for ( int i = 0; i < IndexedNames.size; i++ )

         {

            Console.WriteLine(names[i]);

         }

         Console.ReadKey();

      }

   }

}

Copy after login

When the above code is compiled and executed, it produces the following results:

1

2

3

4

5

6

7

8

9

10

Zara

Riz

Nuha

Asif

Davinder

Sunil

Rubic

N. A.

N. A.

N. A.

Copy after login

Overloading the indexer (Indexer)

The indexer (Indexer) can be overloaded. An indexer can also be declared with multiple parameters, and each parameter can be of a different type. There is no need for the indexer to be integer. C# allows indexers to be of other types, for example, string types.

The following example demonstrates the overloaded indexer:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

using System;

namespace IndexerApplication

{

   class IndexedNames

   {

      private string[] namelist = new string[size];

      static public int size = 10;

      public IndexedNames()

      {

         for (int i = 0; i < size; i++)

         {

          namelist[i] = "N. A.";

         }

      }

      public string this[int index]

      {

         get

         {

            string tmp;

 

            if( index >= 0 && index <= size-1 )

            {

               tmp = namelist[index];

            }

            else

            {

               tmp = "";

            }

 

            return ( tmp );

         }

         set

         {

            if( index >= 0 && index <= size-1 )

            {

               namelist[index] = value;

            }

         }

      }

      public int this[string name]

      {

         get

         {

            int index = 0;

            while(index < size)

            {

               if (namelist[index] == name)

               {

                return index;

               }

               index++;

            }

            return index;

         }

 

      }

 

      static void Main(string[] args)

      {

         IndexedNames names = new IndexedNames();

         names[0] = "Zara";

         names[1] = "Riz";

         names[2] = "Nuha";

         names[3] = "Asif";

         names[4] = "Davinder";

         names[5] = "Sunil";

         names[6] = "Rubic";

         // 使用带有 int 参数的第一个索引器

         for (int i = 0; i < IndexedNames.size; i++)

         {

            Console.WriteLine(names[i]);

         }

         // 使用带有 string 参数的第二个索引器

         Console.WriteLine(names["Nuha"]);

         Console.ReadKey();

      }

   }

}

Copy after login

When the above code is compiled and executed, it will produce the following results:

1

2

3

4

5

6

7

8

9

10

11

Zara

Riz

Nuha

Asif

Davinder

Sunil

Rubic

N. A.

N. A.

N. A.

2

Copy after login

The above is the content of [c# tutorial] C# Indexer, more For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Active Directory with C#

Access Modifiers in C# Access Modifiers in C# Sep 03, 2024 pm 03:24 PM

Access Modifiers in C#

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Random Number Generator in C#

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

C# Data Grid View

C# StringReader C# StringReader Sep 03, 2024 pm 03:23 PM

C# StringReader

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Patterns in C#

C# StringWriter C# StringWriter Sep 03, 2024 pm 03:23 PM

C# StringWriter

BinaryWriter in C# BinaryWriter in C# Sep 03, 2024 pm 03:22 PM

BinaryWriter in C#

See all articles