Real world use cases for C# indexers?

后端 未结 14 1487
时光说笑
时光说笑 2020-12-04 15:26

I\'ve seen lot of examples for c# Indexers, but in what way will it help me in real life situations.

I know the C# guru wouldn\'t have added this if it wasn\'t a ser

相关标签:
14条回答
  • 2020-12-04 16:12
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace IndexerSample
    {
        class FailSoftArray 
        {
            int[] a; // reference to underlying array
            public int Length; // Length is public
            public bool ErrFlag; // indicates outcome of last operation
            // Construct array given its size.
            public FailSoftArray(int size)
            {
                a = new int[size];
                Length = size;
            }
            // This is the indexer for FailSoftArray.
            public int this[int index] 
            {
            // This is the get accessor.
                get
                {
                    if (ok(index))
                    {
                        ErrFlag = false;
                        return a[index];
                    }
                    else
                    {
                        ErrFlag = true;
                        return 0;
                    }
                }
                // This is the set accessor.
                set
                {
                    if (ok(index))
                    {
                        a[index] = value;
                        ErrFlag = false;
                    }
                    else ErrFlag = true;
                }
            }
            // Return true if index is within bounds.
            private bool ok(int index)
            {
                if (index >= 0 & index < Length) return true;
                return false;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                FailSoftArray fs = new FailSoftArray(5);
                int x;
                // Show quiet failures.
                Console.WriteLine("Fail quietly.");
                for (int i = 0; i < (fs.Length * 2); i++)
                    fs[i] = i * 10;
                for (int i = 0; i < (fs.Length * 2); i++)
                {
                    x = fs[i];
                    if (x != -1) Console.Write(x + " ");
                }
                Console.WriteLine();
                // Now, display failures.
                Console.WriteLine("\nFail with error reports.");
                for (int i = 0; i < (fs.Length * 2); i++)
                {
                    fs[i] = i * 10;
                    if (fs.ErrFlag)
                        Console.WriteLine("fs[" + i + "] out-of-bounds");
                }
                for (int i = 0; i < (fs.Length * 2); i++)
                {
                    x = fs[i];
                    if (!fs.ErrFlag) Console.Write(x + " ");
                    else
                        Console.WriteLine("fs[" + i + "] out-of-bounds");
                }
                Console.ReadLine();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 16:15

    Heres a video i have created http://www.youtube.com/watch?v=HdtEQqu0yOY and below is a detailed explanation about the same.

    Indexers helps to access contained collection with in a class using a simplified interface. It’s a syntactic sugar.

    For instance lets say you have a customer class with addresses collection inside it. Now let’s say we would like to like fetch the addresses collection by “Pincode” and “PhoneNumber”. So the logical step would be that you would go and create two overloaded functions one which fetches by using “PhoneNumber” and the other by “PinCode”. You can see in the below code we have two functions defined.

    Customer Customers = new Customer();
    Customers.getAddress(1001);
    Customers.getAddress("9090");
    

    If you use indexer you can simplify the above code with something as shown in the below code.

    Customer Customers = new Customer();
    Address o = Customers[10001];
    o = Customers["4320948"];
    

    Cheers.

    0 讨论(0)
提交回复
热议问题