C # Learning Diary---indexer, overloaded indexer

Source: Internet
Author: User
Indexers allow instances of classes or structs to be indexed like arrays, and when you define an indexer for a class, the class is like an array, and you can use the array accessor ' [] ' to access an instance of that class: Indexers are similar to properties, but indexers take parameters (or, as you might think, indexers are properties of a class and are also implemented by accessors (a special method))





To be more clear, or to give an example, the university is the most leisurely time in life, students can do many of the things they have wanted to do and for various reasons did not do, truancy is obviously one of them. In order to stop the behavior of truancy, every time the teacher should call or point school number, I am 4 class, the teacher likes to learn number, "4 class 1th" and then loudly answer "to ...", "4 class 2nd", "to", "4 class 3rd" ... "Number 3rd. Student number 3rd. "The teacher HC666 to the hospital today" (Chinese good Roommate a ^_^), "Oh, No. 4th" ... The teacher named is an index to class 4.



If CLASS4 is considered to be an object instantiated by the classes class, Class[1] is the index to the number 1th student, (think of the array to understand) how to achieve it?



To define an indexer:



It also says that indexers are similar to attributes, and naturally there are get,set accessors, and indexers are also members of classes, and naturally they have to be defined in the class as follows:

public return value type this [parameter type parameter]
             {
                                 get {return The value specified by the parameter;} // get accessor
                    set {/ * Set the value specified by the parameter * /} // set accessor
            }
Examples of indexers:

We code the above example as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
  
namespace Test1
{// Define a class that can be indexed
    class Classes
    {// The index of the class is essentially the index of the array in the class
       public string [] StudentName = new string [6];
        // Define the indexer
       public string this [int ID]
       {
           get {return StudentName [ID];}
           set {StudentName [ID] = value;} // Set accessor comes with value parameter
         
       }
      
    }
    class Program
    {
        static void Main (string [] args)
        {
            Classes class4 = new Classes ();
            // Index write
            for (int i = 1; i <6; i ++)
            {
                class4 [i] = "HC" + i;
            }
            // Index read
            for (int j = 1; j <6; j ++)
            {
                Console.WriteLine (j + "号 \ t" + class4 [j]);
            }
              
        }
    }
}
result:

Overload indexer:

In the above program, we realized that the student name is indexed by the student number, how to realize the student number by name index? We regard indexing as a special method. The method can use overloading to achieve the same function of different parameters, then the indexer can naturally be overloaded. The usage is similar to method overloading (see the method overloading point here), we still follow the above example, this time to meet the use of name index to exit the student number:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
  
namespace Test1
{// Define a class that can be indexed
    class Classes
    {// The index of the class is essentially the index of the array in the class
       public string [] StudentName = new string [6];
        // Define the indexer
       public string this [int ID]
       {
           get {return StudentName [ID];}
           set {StudentName [ID] = value;} // Set accessor comes with value parameter
         
       }
        // The parameter of the overloaded indexer is set to string type
       public uint this [string name]
       {
           get
           {// Find the student number matching name
               uint index = 1;
               while (StudentName [index]! = name && index <6)
               {index ++;}
               return index;
             
           }
           set {}
       }
      
    }
    class Program
    {
        static void Main (string [] args)
        {
            Classes class4 = new Classes ();
            // Index write
            for (int i = 1; i <6; i ++)
            {
                class4 [i] = "HC" + i;
            }
            // Index read out, index name by student number
            for (int j = 1; j <6; j ++)
            {
                Console.WriteLine (j + "号 \ t" + class4 [j]);
            }
            // Study ID by name index
            for (int k = 1; k <6; k ++)
            {
                string name = "HC" + k;
                Console.WriteLine (name + "\ t" + class4 [name] + "号"); // Compared with the above usage, the parameters are different
            }
        }
    }
}
result:

In the above, we said that when a class defines an indexer, you can treat the class as an array. When learning arrays, you know that arrays have multiple dimensions. What about the class of the indexer? ? Can the foreach traversal statement that we use to traverse the array also work with this class? ? I will introduce it in the next article! ! (Because the school is short on electricity ...) I hope you will continue to support HC666 ^ _ ^

The above is the content of C # Learning Diary 28-Indexer and Overload Indexer. For more related content, please pay attention to topic.alibabacloud.com (www.php.cn)!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.