The indexer inside C # note

Source: Internet
Author: User

1, it is hereby stated that the following code is copied from the full part of the part, may not be able to directly copy execution.

2, the indexer note 1) If you do not set the array to save, cannot continuously access per2[0],per[1], because such as name is replaced. 2) We create an array to save, we need to create space, we can

String[] Arrname=new string[10]; You can also allocate space here
You can also write a construction method that dynamically allocates space, such as the following code.

3, the indexer in addition to the use of examples, the index number is different, to the object of different fields assigned value.

 Public string  This[intIndex] {                Set                {                    if(Index = =0) {Name="test1"; } Else if(Index = =1) Name="Testother"; ElseName=value; }                Get                {                    if(Index = =0)                        returnName; Else if(Index = =1)                        returnName +1; Else                        returnName; } calls//Indexer, cannot access Per2 "0" at once, Per2 "1" Unless return is saved to the arrayPerson Per2 =NewPerson (); per2[0] =" One"; Console.WriteLine (per2[0]); per2[1] =" A"; Console.WriteLine (per2[1]); per2[2] =" -"; Console.WriteLine (per2[2]); Console.WriteLine (per2[0]+" "+per2[1]+" "+per2[2]+" "+per2[3]);

Static voidMain (string[] args) {Person Per2=NewPerson ();p er2[0] =" One"; Console.WriteLine (per2[0]);p er2[1] =" A"; Console.WriteLine (per2[1]);p er2[2] =" -"; Console.WriteLine (per2[2]); Console.WriteLine (per2[0]+" "+per2[1]+" "+per2[2]+" "+per2[3]); Console.readkey ();} Public classperson{intAge ;stringname;//string[] Arrname=new string[10]; You can also allocate space herestring[] arrname;Private BOOLsex; PublicPersonintI//construction method, allocating space for Arrname{arrname=New string[i];} Public string  This[intIndex] {Set{if(Index = =0) {arrname[0] ="test1"; }Else if(Index = =1) arrname[1] ="Testother";ElseArrname[index]=value;}Get{if(Index = =0)returnArrname[index];Else if(Index = =1)returnArrname[index];ElsereturnArrname[index] + -;}}

  

The indexer inside C # note

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.