Declaration of the C # index indicator

Source: Internet
Author: User
Tags modifier

Let's take a look at the declaration format of the index indicator first:

Attributes Indexer-modifiers Indexer-declarator
{Accessor-declarations}

The modifier indexer-modifier that an index indicator can use are:
New
Public
Protected
Internal
Private
Virtual
Sealed
Override
Abstract

A pair of curly braces "{}" is an access declaration between index indicators that uses the GET keyword and the Set keyword to define the read and write permissions to the element being indexed.

For example, the following example prints out a list of team members.

Program Listing 13-4:

Using System;
Class Team
{
 string s_name=new string[8];
 public string This[int nindex]
 {
    get{return
        s_name[nindex];
    }
    set{
        s_name[nindex]=value
}
}} Class Test
{public
 static void Main () {Team
   t1=new team ();
   for (int i=0;i<6;i++)
       Console.WriteLine (T1[i]);
 }

In many cases, some data information should be private to instances of the class or class, and you need to restrict access to that information. And we sometimes do not want such data to be completely closed to the outside world. As with attributes, an index indicator gives us another way to control access rights.

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.