C # attributes and Indexer

Source: Internet
Author: User

 

Using System;

Namespace PropertyIndexerApp
{

Class Class1
{

[STAThread]
Static void Main (string [] args)
{

// Create a MyClass instance
MyClass m = new MyClass ();

For (int I = 0; I <10; I ++)
{
For (int j = 0; j <10; j ++)
{
// Write and read the first Indexer
M [I * 10, j] = I * 10 + j;
Console. Write ("No {0} {1 }:{ 2}", I, j, m [I * 10, j]);
}
Console. WriteLine ();
}

For (int I = 0; I <m. StrCount; I ++)
{// Read the second Indexer
Console. WriteLine (m [I]);
}

// Set instance attributes
M. StrCount = 5;
// Get instance attributes
For (int I = 0; I <m. StrCount; I ++)
{// Read the second Indexer
Console. WriteLine (m [I]);
}

// Read static attributes
Console. WriteLine (MyClass. ClassName );
Console. Write ("Press any key to continue ");
Console. ReadLine ();
}
}

Class MyClass
{
Private const int c_count = 100;
Private static int [] intArray = new int [c_count];
// The first indexer, readable and writable, with two parameters
Public int this [int index, int offset]
{
Get
{
If (index + offset)> = 0 & (index + offset) <c_count)
{
Return intArray [index + offset];
} Else return 0;
}
Set
{
If (index + offset)> = 0 & (index + offset) <c_count)
IntArray [index + offset] = value;
}
}
Private int m_strCount = 3;
Private string [] strArray = {"111", "222", "333 "};
// The second indexer, read-only, one parameter
Public string this [int index]
{
Get
{
If (index> = 0) & (index <m_strCount ))
{
Return strArray [index];
}
Else
{
Return "NULL ";
}
}
}

// Instance attributes, readable and writable
Public int StrCount
{
Get
{
Return m_strCount;
}
Set
{
If (value> m_strCount)
{
StrArray = new string [value];
For (int I = 0; I <value; I ++)
{
StrArray [I] = String. Format ("String No. {0}", I );
}
M_strCount = value;
}
}
}

Private static string m_strName = "MyClass ";
// A static attribute, read-only
Public static string ClassName
{
Get
{
Return m_strName;
}
}
}
}

Definition: in c #, an attribute is a data structure of a class. It is a way to read and write private and protected data in the class. The read/write operations of attributes are performed by Get and Set accessors. Similar to the property function, the class indexer virtualizes a class into an array. It can access the class data in an array to protect and hide the data. The indexer reads and writes are also performed through Get and Set access, but they require parameters.

Analysis: 1. The Declaration format of the indexer is the index value type plus the this keyword, and the parameters in the array subscript format. Multiple indexers can be defined in the same class, but their parameter quantities and types are not exactly the same. Therefore, the indexer can be overloaded to accept different types of parameters and return different types.

2. The indexer completes data operations in get and set statements. It uses parameters in the indexer to read and write private members in the class. In the set statement, value is used to represent the value assignment parameter. Its type is the same as that of the indexer. Access to the indexer members is performed by adding parameters in the array subscript format to the class case.

3. In this example, the class MyClass defines two attributes. One is a static attribute, which can be accessed through the class name and attribute name, and the other is an instance attribute, which can be accessed through the class instance and attribute name.

4. The indexer can only be used in the instance of the class and cannot be declared as static. Attributes can be either instance or static.

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.