C # GET, set, indexer

Source: Internet
Author: User

Get and set

The properties of a C # class have public properties and private properties (private). If you declare a property as public directly, any instance of the class can arbitrarily get or modify the value of the property, which is not safe. In the. NET Framework, we use the Get keyword to get the value of a common property, and set to use the value of a public property. In Get or set, you can write code to control the acquisition and modification of property values.

 Public classsplaybowclass{//. net2.0 and version 1.1 how to set properties    PrivateString name;//Private Properties     PublicString Name {//The public property corresponding to the name of this private property    Get{return  This. Name; }Set{ This. Name =value; }    }    //. After net3.5, the effect is the same as above     Public intport{Get;Set;}}

Attention:

Get and set can appear according to the actual needs of one, not necessarily two must appear.

The name of the public attribute in the example and the name of the private property are the difference in case, which is just a writing habit. Others like to add an underscore (_) after a private attribute.

Definition of indexers

An indexer allows an instance of a class or struct to be indexed in the same way as an array, with an indexer similar to an attribute, unlike an indexer whose access is a parameter. Declarations are somewhat similar to attributes. The indexer is defined without a name, but with the This keyword, which points to an object instance. The syntax is as follows:

 This [Type index]    {        //  get accessor        Get         {            //  Returns the value specified by index         }         //  set accessor Set                {            //  Sets the value specified by index         }        }

Where: Element-type refers to the type of the element, type refers to the index, can be string, int, and so on.

classdaycollection{string[] days = {"Sun","Mon","Tues","Wed","Thurs","Fri","Sat" }; Private intGetDay (stringtestday) {         for(intj =0; J < days. Length; J + +)        {            if(Days[j] = =testday) {                returnJ; }        }    }     Public int  This[stringDay//the definition of the indexer, the element type is int, the index type is string    {        Get //only Get property, no set property        {            return(GetDay (day)); }    }}
Use of indexers

The declaration of the behavior of the indexer is somewhat similar to the property. Just like attributes, you can use get and set accessors to define indexers. However, the property returns or sets a specific data member, and the indexer Returns or sets a specific value for the object instance. In other words, it divides the instance data into smaller parts and indexes each part to get or set each part. You can use the array access operator ([]) to access an instance of the class or struct.

  

usingSystem;usingSystem.Collections; Public classindexerclass{Private string[] name =New string[2];  Public string  This[intIndex//the indexer must be defined with the This keyword, in fact this is the object after the class instantiation    {        //implement the Get method of the indexer        Get        {            if(Index <2)            {                returnName[index]; }            return NULL; }        //implementing the Set method of the indexer        Set        {            if(Index <2) {Name[index]=value; }        }    }} Public classtest{Static voidMain () {//use of indexersIndexerclass Indexer =NewIndexerclass (); indexer[0] ="Tom Brown";//The index is assigned to the right of the "=" number, which is actually called its set methodindexer[1] ="Jim Green"; Console.WriteLine (indexer[0]);//the value of the output indexer is actually called its Get methodConsole.WriteLine (indexer[1]); }}

C # GET, set, indexer

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.