c#索引程序

c#索引程序,c#,C#,此程序中有错误。有人可以修复吗 错误是:TempRecord已使用相同的参数值定义了名为“this”的成员 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 命名空间控制台应用程序6 { 课堂临时记录 { //温度值数组 私有浮动[]临时=新浮动[10]{56.2F,56.7F,56.5F,56.9F,58.8F, 61.3F、65.9F、62.1F、59.2F、57.5F}; 私有int[]d=新int[10]{4,5,5,4

此程序中有错误。有人可以修复吗

错误是:TempRecord已使用相同的参数值定义了名为“this”的成员

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序6
{
课堂临时记录
{
//温度值数组
私有浮动[]临时=新浮动[10]{56.2F,56.7F,56.5F,56.9F,58.8F,
61.3F、65.9F、62.1F、59.2F、57.5F};
私有int[]d=新int[10]{4,5,5,4,4,43,2,2,5,3};
//启用客户端代码以验证输入
//访问索引器时。
//公共整数长度
//{
//获取{return temps.Length;}
//}
//索引器声明。
//如果索引超出范围,temps数组将抛出异常。
公共浮动此[int索引]
{
得到
{
返回临时[索引];
}
设置
{
temps[索引]=值;
}
}
public int this[int index]//错误:TempRecord已使用相同的参数值定义了名为“this”的成员
{
得到
{
返回d[索引];
}
设置
{
d[指数]=数值;
}
}
}
班级计划
{
静态void Main(字符串[]参数)
{
TempRecord TempRecord=新的TempRecord();
//使用索引器的set访问器
tempRecord[3]=58.3F;
tempRecord[5]=60.1F;
//使用索引器的get访问器
对于(int i=0;i<10;i++)
{
WriteLine(“元素{0}={1}”,i,tempRecord[i]);
}
Console.WriteLine(tempRecord[2]);
//在调试模式下保持控制台窗口打开。
System.Console.WriteLine(“按任意键退出”);
System.Console.ReadKey();
}
}
}

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

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

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

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

如果两个成员都返回不同的类型,您可能会认为您能够做到这一点,就像您的一样。但这会给编译器带来歧义。如果您有这样的代码,会调用哪个成员

object x = tempRecord[3];
使一个或两个索引器成为方法

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

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

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

                set
                {
                    d[index] = value;
                }
            }