C # indexer program

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

Error: TempRecord already defines a member named 'this' with the same parameter value

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    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]//error:TempRecord already defines a member called 'this' with the same parameters value
        {
            get
            {
                return d[index];
            }

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

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



            // Use the indexer 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();

        }
    }
}
+3
source share
3 answers

You have two members with a name thisthat take the same parameters. This is not allowed in C # (or other .Net languages, as far as I know).

You think you can do this if both members return different types, like yours. But that would be a compiler with ambiguity. Which member will be called if you have such code?

object x = tempRecord[3];

Make one or both indexers a method.

+1
source

, , - 2 . #.

public int GetD(int index) {
  return d[index];
}

public void SetD(int index, int value) {
  d[index] = value;
}
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    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 };

        public int Length //
        {
            get { return temps.Length; }
        }
        public float this[int index]
        {
            get { return temps[index]; }

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

    class Program
    {
        static void Main(string[] args)
        {
            TempRecord tempRecord = new TempRecord();
            tempRecord[3] = 58.3F;
            tempRecord[5] = 60.1F;


            for (int i = 0; i < 10; i++)
            {
                System.Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]);
            }
            Console.WriteLine(tempRecord[2]);
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();

        }
    }
}

, , , . , , this , .

( ), , .

, - foll. , temprecord.d[index Value] d .

public int d[int index]
            {
                get
                {
                    return d[index];
                }

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

Source: https://habr.com/ru/post/1763286/


All Articles