Structure and indexer of Reading Notes in. NET Framework programming

Source: Internet
Author: User
Tags define get
I. Structure and indexer (also known as including parameter attributes) Class classstruct
{
Struct mystruct
{
Public String [] strtest;
Public String This [int Index]
{
Get
{
Return strtest [Index];
}
Set
{
Strtest [Index] = value;
}
}
}
Public static void main ()
{
Mystruct MS = new mystruct ();
Ms. strtest = new string [2];
MS [0] = "AAAAA ";
The MS [1] = "bbbbb ";
Console. writeline ("the first is {0}, the second is {1}", MS [0], MS [1]);
}
}
// Suitable for processing a large amount of data, such as reading a record from a database and performing operations

1. Add the public keyword before the structure member declaration
2. structure members cannot be initialized when the structure is declared.
3. attributes in the structure cannot be passed as ref or out parameters because they do not allocate storage space.
4. The structure can implement interfaces.
Class can also have an index (including parameter attributes) public class classindex
{
Public Boolean this [int I]
{
Get {;}
Set {;}
}
}
// The access method is
Classindex CI = new classindex (5 );
CI [0]
CI [1]
CI [2]
CI [3]
CI [4]
CI [5]
// Define get and set to perform value and value assignment.
// 1.c# you cannot specify a name for the indexer.
Automatically specified as get_item set_item
// 2. system. runtime. compilerservices. indexernameattribute can specify the name of the indexer. However, name locating indexer is not supported in C #.
[System. runtime. compilerservices. indexername ("myname")]
Public Boolean this [int I]
{
// The index is named set_myname get_myname, instead of set_item get_item.
}
// System. String is an indexer with a changed name. Its name is Chars rather than item. In this way, we can obtain a single character in a string.

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.