简体   繁体   中英

program of Indexers in c#

There is an error in this program.can anyone fix this?

class TempRecord
{
    // Array of temperature values
    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 };
    private int[] d= new int[10]{4,5,5,4,4,43,2,2,5,3};
    // To enable client code to validate input 
    // when accessing your indexer.
    //public int Length
    //{
    //    get { return temps.Length; }
    //}
    // Indexer declaration.
    // If index is out of range, the temps array will throw the exception.
    public float this[int index]
    {
        get
        {
            return temps[index];
        }

        set
        {
            temps[index] = value;
        }
    }
    public int this[int index]
    {
        get
        {
            return d[index];
        }

        set
        {
            d[index] = value;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        TempRecord tempRecord = new TempRecord();
        // Use the indexer's set accessor
        tempRecord[3] = 58.3F;
        tempRecord[5] = 60.1F;



        // Use the indexer's get accessor
        for (int i = 0; i < 10; i++)
        {
            System.Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]);
        }
        Console.WriteLine(tempRecord[2]);
        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();

    }
}

Type 'ConsoleApplication1.TempRecord' already defines a member called 'this' with the same parameter types

Is that your error?

If it is - read what it says =) You have two this members, also known as Indexers , with identical method signatures. Specifically:

public float this[int index]
public int this[int index]

Remember that for disambiguation, the return type is not considered. You need to either remove one completley or change it to a method, rather than an indexer.

Imagine if it was allowed and you had the following code:

var record = new TempRecord();
object value = record[3];

Which indexer should be called here? The one that returns a float or the one that returns an int . Yes, this example is contrived but from a language design and compiler implementation perspective it's perfectly valid and thus the reason you can't overload by return type.

As GenericTypeTea said, saying that you have some error is too vague to expect any kind of comprehensive response. However, right off the bat I can tell you that you are attempting to overload the indexer property based solely on the return type, which is not possible in C# (or any polymorphic language I know of, for that matter).

Also, providing both an array size and an initializer list at the same time is asking for maintenance headaches later on. This code compiles because the number of elements in the initializer list and the size match, but if you change one and forget to change the other, you will start getting compiler errors. Personally, I would advise using empty brackets if you are using an initializer list, but I suppose that can be chalked up to personal preference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM