directory

What is an indexer?

How do I declare indexers?

Similarities and differences between indexers and attributes

Indexer instance analysis


Hello! Hi, I’m a grey ape trying to make money to buy hair tonic!

Indexing functions are used in C# development recently, so today I will record the implementation of the indexer in C# with my friends.

 

What is an indexer?

In c #, the indexer allows an instance of the class or structure according to the and array in the same way as indexes, the indexer similar to attributes, the difference is that their access to the parameters, and, in fact, the indexer provides a method of access to the class or structure that allowed in accordance with the structure, and the class or interface in the same way as indexes, It was introduced to make the program more intuitive and easy to understand.

 

How do I declare indexers?

Indexers provide a special method for assigning and accessing objects in a class or structure. To declare indexers on a class or structure, use the this keyword. Indexers are defined in the following format:

[The modifier] data typethis[index type index] {get{// Get the attribute code}
    set// Set the property of the code}
}
Copy the code

 

Modifiers include: public, protected, private, internal, new, virtual, sealed, override, the abstract, extern.

A data type is a type that represents an array or collection element to be accessed, such as string, int, and so on.

The this keyword refers to an instance of the current class, and as you can see, it is provided with set and GET accessors for the indexer as well as for normal attributes, and these accessors specify what internal members will be referenced when the indexer is used.

The indexer type indicates what type of index the indexer uses to access an array or collection element, which can be an integer or a string.

 

Similarities and differences between indexers and attributes

Indexers are similar to attributes by definition, with the following similarities:

  • They are all function members and do not need to be allocated memory to store them.
  • They are primarily used to access, associate with, and provide access to other data members.

 

The difference between indexers and attributes is as follows:

  • Properties allow methods to be called as if they were public data fields, and indexers allow methods of objects to be called as if the object were an array.
  • Properties can be accessed by simple names, and indexers can be accessed by indexers.
  • Properties can be static or instance members, and indexers must be instance members.
  • Property’s GET accessor has no parameters. Indexer’s GET accessor has the same parameter list as the indexer.
  • The indexer’s set accessor has the same parameter list as the indexer in addition to the value argument.

 

Indexer instance analysis

For example, in this program,

Define a class Couse, in the class to define three private members Chinese, English, Math, through the index function to access the private members of the class, and these three private members read and write operations.

 

The first step is to define the Couse class and implement the indexer in it:

// Define the class Couse
class Couse
{
// Define private member variables
        private float Chinese;
        private float English;
        private float Math;

// Define an indexer whose modifier is public, data type is float, and index type is string
        public float this[string name]
        {
// Define the get method to read the member
            get
            {
                switch (name)
                {
                    case "Chinese":
                        return Chinese;                     
                    case "English":
                        return English;                       
                    case "Mathematics":
                        return Math;      
                    default:
                        return 0; }}// Define the set method to write to the member
            set
            {
                switch (name)
                {
                    case "Chinese":
                        Chinese = value;
                        break;
                    case "English":
                        English = value;
                        break;
                    case "Mathematics":
                        Math = value;
                        break; }}}}Copy the code

 

Call the Couse class and indexer in the main class to read and write member variables:

class Program

    {
        static void Main(string[] args)
        {
            // Create a new object to accept the Couse class
            Couse couse = new Couse();
            couse["Chinese"] = 100;  // Assign to Chinese through the "language" index
            couse["English"] = 99;   // Assign English through the "English" index
            couse["Mathematics"] = 98;   // Assign to Math through the "Math" index

            // Get the value of the member by the corresponding index and print it
             Console.WriteLine("Language:" + couse["Chinese"]);
             Console.WriteLine("English:" + couse["English"]);
             Console.WriteLine("Mathematics:" + couse["Mathematics"]); }}Copy the code

The running results are as follows:

In the above program, you define the Couse class and set the indexer in it, then call the class in the main method and assign the value to the member variable through the index, and then call the corresponding member variable through the index.

Well, it’s not much to understand.

 

Remember to click “like” to follow the big bad Wolf!

At the same time, you can also follow my wechat public account “Grey Wolf Hole master” back to “Python Notes” to get Python from the beginning to master notes sharing and Python common functions and methods quick check manual!

Big bad Wolf is looking forward to progress with you!