索引器的c#程序

时间:2010-09-05 02:26:02

标签: c#

此程序中存在错误。任何人都可以解决此问题吗?

  

错误是:TempRecord已经定义了一个名为'this'的成员,其参数值相同

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'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();

        }
    }
}

3 个答案:

答案 0 :(得分:1)

您有两个名为this的成员,它们使用相同的参数。据我所知,这在C#(或其他.Net语言)中是不允许的。

如果两个成员返回不同的类型,你认为你可以这样做,就像你的那样。但这会给编译器带来歧义。如果您有这样的代码,将会调用哪个成员?

object x = tempRecord[3];

使一个或两个索引器成为一种方法。

答案 1 :(得分:0)

您要做的是让2个索引器具有相同的参数和不同的返回类型。这在C#中是不合法的。您需要将其中至少一个移动到函数中

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

public void SetD(int index, int value) {
  d[index] = value;
}

答案 2 :(得分: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]来访问&amp;使用main中的成员d。

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

                set
                {
                    d[index] = value;
                }
            }