Tag:ar using on data bs code ad as line
Indexers are a special class member that allows objects to be accessed in an array-like manner, making the program look more intuitive and easier to write. 1. The definition of an indexer a class member in C # can be any type, including arrays and collections. When a class contains arrays and collection members, the indexer greatly simplifies the access operations of an array or collection member. You define indexers in a way that is similar to defining a property, in the following general form: [modifier] Data type this[index type index]{get{//get Property Code} set{//Set Property Code}} modifier includes public,protected,private, Internal,new,virtual,sealed,override, Abstract,extern. The data type is the type that represents the array or collection element that will be accessed. The indexer type indicates which type of index the indexer uses to access an array or collection element, which can be an integer, which can be a string, and this represents an array or collection member that operates on this object, which can simply be understood as the name of the indexer, so the indexer cannot have a user-defined name. For example: Class z{//integer set with 100 integers private long[] arr = new long[100];//Declaration Indexer public Long This[int index] {get {//check indexed range if (i Ndex < 0 | | Index >=) {return 0;} else {return arr[index];}} set {if (!) ( Index < 0 | | Index >=) {Arr[index] = value;}} The use of}2, indexers can access an array member of an instance of a class by means of an indexer, which is similar in general form to the following: Object name [index] where the data type of the index must be the same as the index type of the indexer. For example: Z z=new z (); z[0]=100;z[1]=101; Console.WriteLine (Z[0]); indicates that an object z is created first, and then the array elements in the object are referenced by an index. 3. An indexer in an interface can also declare an indexer in an interface, and there are two differences between an interface indexer and a class indexer: one is that the interface indexer does not use modifiers, and the other is that the interface indexer contains only accessors get or set, and there are no implementation statements. The purpose of the accessor is to indicate whether the indexer is read-write, read-only, or write-only, and if it is read-write, the accessor get or set cannot be omitted, if read-only, the set accessor is omitted, and if it is write-only, the get access is omitted.Manager For example: public interface iaddress{string This[int Index]{get;set;} String Address{get;set;} String Answer ();} Indicates that the declared interface Iaddress contains 3 members: an indexer, an attribute, and a method in which the indexer is readable and writable. 4, the indexer and the property of the comparison indexer and properties are members of the class, syntactically very similar. Indexers are typically used in custom collection classes to manipulate collection objects by using indexers as easily as arrays, whereas properties can be used with any custom class, which enhances the flexibility of the class's field members. The property indexer allows a method to be called, as if a public data member allows a method on the calling object, as if the object is an array accessible through a simple name, accessible through the indexer can be accessed by a static member or an instance member must be an instance member whose get accessor has no parameters whose get accessor has the same formal parameter as the indexer A table whose set accessor contains an implicit value parameter, in addition to the value parameter, has the same formal parameter list as the indexer
[Go]c# indexer