C # language series lecture (8) indexer and operator overload

xiaoxiao2021-03-06  74

Indexer Indexer (Indexer) is a new class member introduced in C #, which makes it easy and intuitively referenced like arrays. The indexer is very similar to the previously mentioned attributes, and the indexer can have a list of parameters, but only on the instance object, and cannot act directly on the class. The indexer does not have the name like attributes and methods, and the keyword THIS clearly expresses the characteristics of the indexer reference object. Like attributes, Value keyword has the meaning of parameter delivery in the statement block after SET. In fact, from the compiled IL intermediate language code, the SET and GET functions in this indexer are implemented for both methods of GET_ITEM (INDEX) and SET_ITEM (INDEX, OBJECT VALUE), so they can no longer be indexed The category of the unit declares these two methods, the compiler will report this behavior. The method of such implicit implementations can also be invoked, inherited, etc., which is the same as the method implemented in the code. Like the method, the indexer has five access protection levels and four inheritance behavior modifications, and external indexers. These behaviors do not have any differences with methods, and will not be described here. The only thing is that the indexer cannot be static. It is worth noting that when override implements an indexer, it should be used to access the parent class's indexer with Base [E]. Like the realization of attributes, the data type of the indexer simultaneously provides the type of the GET statement block and the type of the Value keyword in the SET statement block. The parameter list of the indexer is also a matter of paying attention. The "index" features make the indexer must have at least one parameter, which is within the parentheses after the THIS keyword. The parameters of the indexer can only be a pass value, and there is no REF (reference) and OUT modification. The data type of the parameter can be any data type in C #. C # is based on different parameter signs to perform the polymorphism of the indexer. All parameters in parentheses can be referenced under GET and SET, and the value keyword can only be used as a delivery parameter under Set.

Below is a specific application of an indexer: use system; class bitArray {int [] bits; intlength; public bitArray (int length) {if (Length <0) throw new argumentException (); bits = new int = ((Length " - 1) >> 5) 1]; this.Length = Length;} public int length {get {return length;}} public bool this [int index] {get {ified (index <0 || index> = length Throw new indexoutofrange- Exception (); Else Return (bits [index >> 5] & 1 << index)! = 0;} set {if (INDEX <0 | INDEX> = Length) throw new indexoutofrange- Exception ); 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] == 0; for (int i = 0; i

Here is a specific example: use system; class complex {double r, v; // r vi public complex (double r; double v) {this.r = r; this.v = v;} public static complex Operator (Complex A, Complex B) {RETURN New Complex (A.R Br, A.V BV);} public static complex operator- (complex a) {return new complex (-ar, -AV);} public static Complex Operator (Complex a) {double r = a.r 1; double v = a.v 1; return new complex (r, v);} public void print () {Console.Write (R " " V "i");}} class test {public static void main () {complex a = new complex (3,4); complex b = new complex (5, 6); complex c = -a; c.print ); Complex d = a b; d.Print (); a.print (); complex e = a ; a.print (); E.Print (); complex f = a; a.print () F.Print ();}} Compiler and runs, can get the following output: -3 -4i 8 10i 3 4i 4 5i 3 4i 5 6i 5 6i here to achieve a " " The binary operator, a "-" one yuan operator (with a negative value) and a " " one yuan operator. There is no change in the parameters of the collections here - this is especially important when parameters are reference type variables, pay attention to the parameters of the overload operator can only be a pass value. When returning a value, "New" often needs a new variable (except true and false operators), which is often used by the " " and "-" operator, that is, when doing "A " The original a value will be discarded, and the value of the new "new" comes out! Operator overload has a quite strict requirement for return values ​​and parameter types: only one parameter in one yuan operator; operator " " and "-" The return value type and parameter type must be the same as the type of the operator; the number of operators " -! ~" Must be the same as the type of the operator, but the return value type can be arbitrary; True and The parameter type of the FALSE operator must be the same as the type of the operator, and the return value type must be BOOL, and must be paired - that is, the one is not correct, it will cause compilation errors. The difference in parameter type will result in overloading of the same name operator - actually this is the manifestation of method overload. The binary operator parameter must be two, and at least one parameter type is to declare the type of the operator, and the return value type can be arbitrary. Three pairs of operators must be paired, they are "==", "! =", ">" And "<", "> =" and "<=". It should be noted that the types of two parameters are different, or although the types of types are the same but the order is different, they can cause overloading of the same name operator. The conversion operator provides implicit conversion and explicit conversion between different types, mainly for method call, transformation expression, and assignment operation.

转载请注明原文地址:https://www.9cbs.com/read-121284.html

New Post(0)