C # Tutorial Lesson 11th: Index Indicator

Source: Internet
Author: User
Tags array count create index empty include int size integer
Tutorials | Index This lesson describes C # 's index indicators, which are designed to include:
1. Learn what an index indicator is

2. How to implement index indicators

3. Overloaded Index Indicator

4. Learn how to implement index indicators with multiple parameters

Index indicators are not difficult to use. Their usage is the same as the array. Within a class, you can manage a collection of sets of data according to your wishes. These objects can be a finite set of class members, another array, or some complex data structure. The internal implementation of the class is not considered, and its data can be obtained by using an index indicator. The following is an example:

1. Listing 11-1. Example of an index indicator: IntIndexer.cs

Using System;
///
A Simple Indexer Example.
///
Class Intindexer
{
Private string[] MyData;

public intindexer (int size)
{
MyData = new String[size];
for (int i=0 i < size; i++)
{
Mydata[i] = "EMPTY";
}
}
public string This[int POS]
{
Get
{
return Mydata[pos];
}
Set
{
Mydata[pos] = value;
}
}

static void Main (string[] args)
{
int size = 10;
Intindexer myind = new Intindexer (size);
MYIND[9] = "Some Value";
MYIND[3] = "Another Value";
MYIND[5] = "any Value";
Console.WriteLine ("\nindexer output\n");
for (int i=0 i < size; i++)
{
Console.WriteLine ("Myind[{0}]: {1}", I, myind[i]);
}
}
}

Description

1. Listing 11-1 shows how to implement an index indicator, the Intindexer class has an array of strings named MyData, which is a private member, so that its external members are invisible. The array is initialized in a constructor with an integer size parameter to initialize the MyData array, and the word "empty" as the value of each array element when initialized.

The next member of the 2.IntIndexer class is the index indicator (Indexer), identified by the keyword this and square brackets [int pos]. The member has a positional parameter pos. As you have guessed, indexer implementations are the same as attributes. Indexer has get and set access operations, just as in the properties. The index indicator (indexer) returns a string that, when the index indicator is defined, indicates that its return type is a string type.

The 3.Main () method accomplishes the following: Initializes a new Intindexer object, adds some values, and prints out the results. The output results are as follows:

Indexer Output

Myind[0]: Empty
MYIND[1]: Empty
MYIND[2]: Empty
MYIND[3]: Another Value
MYIND[4]: Empty
MYIND[5]: Any Value
MYIND[6]: Empty
MYIND[7]: Empty
MYIND[8]: Empty
MYIND[9]: Some Value

4. In many programming languages, an integer is usually used as a subscript to access the elements of an array, but C # 's index indicator not only does that, but it can go further. When you define an index indicator, you can have multiple parameters, each with a different type. The parameters you add are separated by commas, the same as the parameter tables in the method. The valid parameter types for index indicators include: integer, enum type, and string. In addition, index indicators can be overloaded. In Listing 11-2, we modified the previous program to overload the index indicator so that different types of parameters can be accepted.

2. Listing 11-2. Overloaded Index Indicator: OvrIndexer.cs

Using System;
///
Implements overloaded Indexers.
///
Class Ovrindexer
{
Private string[] MyData;
private int arrsize;
public ovrindexer (int size)
{
arrsize = size;
MyData = new String[size];
for (int i=0 i < size; i++)
{
Mydata[i] = "EMPTY";
}
}

public string This[int POS]
{
Get
{
return Mydata[pos];
}
Set
{
Mydata[pos] = value;
}
}

public string this[string Data]
{
Get
{
int count = 0;
for (int i=0 i < arrsize; i++)
{
if (mydata[i] = = data)
{
count++;
}
}
return count. ToString ();
}
Set
{
for (int i=0 i < arrsize; i++)
{
if (mydata[i] = = data)
{
Mydata[i] = value;
}
}
}
}

static void Main (string[] args)
{
int size = 10;
Ovrindexer myind = new Ovrindexer (size);
MYIND[9] = "Some Value";
MYIND[3] = "Another Value";
MYIND[5] = "any Value";
myind["EMPTY"] = "no value";
Console.WriteLine ("\nindexer output\n");
for (int i=0 i < size; i++)
{
Console.WriteLine ("Myind[{0}]: {1}", I, myind[i]);
}
Console.WriteLine ("\nnumber of \" No value\ "entries: {0}", myind["no value"]);
}
}

Description

1. Listing 11-2 shows how to overload an index indicator.

The first index indicator with integral parameter pos is the same as in Listing 11-1, but there is a new index indicator with string arguments in the program. For this new index indicator, the get operation returns the number of members that match the parameter value data. The set operation changes the value of the element that matches the value of the parameter in the array to a value.

2. In Listing 11-2, in the main () method, an overloaded index indicator is shown, which takes a string parameter.

The overloaded index indicator invokes the set operation by using the following command: myind["EMPTY"] = "no value"; The set operation assigns a value of "no value" to a member in the Myind class that has a value of "empty". After each member of the Myind class has been exported, the last data is output to the console, which counts the number of "no value" for the array member value. You can invoke a get operation by using the following command: myind["no value"]. The output results are as follows:

Indexer Output
MYIND[0]: No value
MYIND[1]: No value
MYIND[2]: No value
MYIND[3]: Another Value
MYIND[4]: No value
MYIND[5]: Any Value
MYIND[6]: No value
MYIND[7]: No value
MYIND[8]: No value
MYIND[9]: Some Value

Number of "No value" entries:7

3. In Listing 11-2, two index indicators coexist in the same class, which is possible because they have different characteristics.

The characteristics of an index indicator are expressed by the number and type of parameters in the index indicator parameter table. class to identify its characteristics and invoke the corresponding index indicator. Index indicators with multiple parameters can be implemented in the following format:

public object This[int param1, ..., int paramn]
{
Get
{
Process and return some class data
}
Set
{
Process and assign some class data
}
}

Summary
Now you've learned what index indicators are for, and how to use them. As with the use of arrays, you can create index indicators to access members of a class. The overload and Multi-parameter index indicators for index indicators are also mentioned in this article.



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.