C # Beginner's indexer (Indexer)

Source: Internet
Author: User

Note: Most of the content below is for online information or other friends ' blogs Guest Excerpt

I am a beginner. NET, I hope that the predecessors to teach and exchange

An indexer (Indexer) is a new class member introduced by C # that makes an object in a class as easy and intuitive to reference as an array. Indexers are very similar to properties, but indexers contain index parameters (within []), and indexers are non-named (because this is indexed, so the "name" used in the declaration is this) and can only be applied on instance objects, not directly on the class. A class that defines an indexer allows you to access members of a class using the [] operator, just as you would access an array.

An example is given below:

Look at the code, here is the definition of the class, with an indexer definition in the middle

public class Person
{
Define two fields of information
private string name;
private string password;

Define a Name property to manipulate the name field
public string Name
{
set {name = value;}
get {return name;}
}

Define a Password property to manipulate the Password field
public string Password
{
set {password = value;}
get {return password;}
}

Defines the indexer, the Name field has an index value of 0, and the index value of the password field is 1
public string This[int Index]
{
Get
{
if (index = = 0) return name;
else if (index = = 1) return password;
else return null;
}
Set
{
if (index = = 0) name = value;
else if (index = = 1) password = value;
}
}
}

Here's how to use indexers:

protected void Page_Load (object sender, EventArgs e)
{
Declare and instantiate this class
Person p = new person ();

Using indexers to assign values to two properties of a class
P[0] = "Jarod"; As you can see, the fields in the object can be accessed as easily as an array, "Here's the assignment for name and password."
P[1] = "123456,./";

Use class properties to get two field information
Label1.Text = P.name + "/" + P.password;
}

So far, I think we should have a general grasp, then there are some details to add:

(1) Indicates whether the user can define the name of the index indicator.

A: Cannot use user-defined name is this "index table"

(2) How to avoid the problem of array crossing when using index indicator

This is a question for friends to answer.

(3) Use index indicator to bring us those convenience

A: You can not completely close a field (similar to a property), access the field value as if you were using an array, or modify

————————————— Learning without thinking is a myth, but thinking without learning is ———————————————————

C # Beginner's indexer (Indexer)

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.