A summary of the indexer knowledge points in C #

Source: Internet
Author: User


An indexer (Indexer) is a new class member introduced by C # that makes an object as convenient and intuitive as an array. Indexers are very similar to the properties mentioned earlier, but indexers can have argument lists and can only act on instance objects, not directly on the class. The following is the design of a typical indexer, where the specific implementation is ignored.

Class MyClass
 {Public        
          object this [int index]      
        {                
                  get        {            //Fetch data        }               
                  set         {            //Save data c10/>}}}
      


Indexers do not have names like properties and methods, which clearly express the characteristics of the indexer reference object. As with attributes, the value keyword has parameter-passing meaning in the statement block after set. In fact, from the compiled Il Intermediate Language code, the above indexer is implemented as: Class MyClass

{Public          
             object get_item (int index)         
            {          //Fetch data    } public         
            void set_item (int index,object value) c19/>{//Save Data    }

Because the indexer is compiled behind the get_item (int index) and set_item (int index, object value) Two methods, and can not even declare the implementation of the two methods in the class that declares the indexer, the compiler will complain about the behavior. This implied implementation of the method can also be invoked, inherited and other operations, and its own methods to achieve identical. Familiarity with the low-level compiler implementation of the C # language provides a good basis for understanding the behavior of the C # indexer below. As with methods, indexers have 5 access protection levels, and 4 inheritance behavior modifiers, as well as external indexers. There is no difference between these acts and the method, and there is no more to repeat. The only difference is that indexers cannot be static (static), which is easy to understand in the semantics of object references. It is worth noting that when overriding (override) implements indexers, you should use Base[e to access the indexer of the parent class. The data type of the indexer, like the implementation of the property, is both the return type of the Get statement block and the type of the value keyword in the SET statement block. The parameter list of indexers is also noteworthy. The characteristics of the index make it necessary for the indexer to have at least one parameter, which is in brackets after the This keyword. The parameters of an indexer can only be of a passing value type, and there can be no ref (reference) and out (output) adornments. The data type of the parameter can be any data type in C #. C # According to different parameters of the signature of the indexer polymorphism discrimination. All the parameters in the brackets are referenced under get and set, and the value keyword can only be passed as a parameter under set. The following is a specific example of an indexer that is useful for understanding the design and application of indexers.

Using System; Class bitarray{int[] bits int length; public BitArray (int length) {if (length < 0) throw new Argumentexcepti
      On ();
     bits = new int[((length-1) >> 5) + 1];
this.length = length; public int Length {get {return Length;} public bool This[int index] {get {if (index < 0 | |
         Index >=length) throw new IndexOutOfRangeException ();
      else return (Bits[index >> 5] &1 << index)!= 0; set {if (Index < 0 | | | index >=length) throw newindexoutofrangeexcept
            Ion ();
            else if (value) Bits[index >> 5] |= 1 <<index;
       else Bits[index >> 5] &= ~ (1<< index); }} class Test {static void Main () {BitArray bits=new BitArray (10)
                     ;
              for (int i=0;i<10;i++)              bits[i]= (i%2) ==bsp;
          Console.Write (bits[i]+ ""); }
}


Compile and run the program to get the following output: True false True Falsetrue false true False

The above program provides the user with an interface-friendly bool array with the use of indexers, while significantly reducing the cost of storage space for the program. Indexers are often used in object containers to provide a friendly access interface to the objects within them-which is why C # wraps the methods into indexers. In fact, you can see that indexers have a large number of applications in the. NET Framework class Library.


The above is my knowledge of the index in C # in the summary, if there is a contradiction, please criticize the advice.

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.