C # basic -- Indexer

Source: Internet
Author: User

The indexer syntax facilitates the creation of client applications as classes, structures, or interfaces for array access.

The indexer is mainly used to encapsulate internal sets or arrays. Use the this keyword.

  1. class TempRecord{    private float[] temps=new float[10]{56.2F, 56.7F, 56.5F, 56.9F, 58.8F,                                             61.3F, 65.9F, 62.1F, 59.2F, 57.5F};    public int Length    {        get{return temps.Length;}    }        public float this[int index]    {        get{return temps[index];}        set {temps[index]=value;}    }}public class MyClass{    public static void Main()    {        TempRecord tempRecord=new TempRecord();        tempRecord[3]=58.3F;        tempRecord[5]=60.1F;              for(int i=0;i<tempRecord.Length;i++)        {            Console.WriteLine("Element #{0} = {1}", (i+1), tempRecord[i]);        }               Console.ReadLine();    }}

Output:

 

C # The Index type is not limited to integers:

  1. class DayCollection{    string [] days={ "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };    private int GetDay(string testDay)    {        int i = 0;        foreach (string day in days)        {            if (day == testDay)            {                return i;            }            i++;        }        return -1;    }    public int this[string day]    {        get{return (GetDay(day));}    }}public class Program{    public static void Main()    {        DayCollection week=new DayCollection();        Console.WriteLine(week["Fri"]);        Console.WriteLine(week["Made-up Day"]);        Console.ReadLine();    }}

Output:

 

Use the indexer on the interface:

  1. public interface IIndexInterface{    int this[int index]    {        get;        set;    }}class IndexClass:IIndexInterface{    public int [] arr=new int[100];    public int this[int index]    {        get        {            if (index < 0 || index >= 100)            {                return 0;            }            else            {                return arr[index];            }        }        set        {            if (!(index < 0 || index >= 100))            {                arr[index] = value;            }        }    }}public class Program{    public static void Main()    {        IndexClass test=new IndexClass();        test[2]=4;        test[5]=32;        for(int i=0;i<10;i++)        {            Console.WriteLine("Element #{0} = {1}", i, test[i]);        }        Console.ReadLine();   }}

Output:

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.