Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对c类中的多个数组使用索引器#_C# - Fatal编程技术网

C# 对c类中的多个数组使用索引器#

C# 对c类中的多个数组使用索引器#,c#,C#,我的基类中有两个数组,我想创建可以在这两个数组中使用的索引器,下面是我尝试做的MVCE class Indexer { private string[] namelist = new string[size]; private char[] grades = new string[size]; static public int size = 10; public IndexedNames() { for (int

我的基类中有两个数组,我想创建可以在这两个数组中使用的索引器,下面是我尝试做的MVCE

class Indexer
      {
      private string[] namelist = new string[size];
      private char[] grades = new string[size];
      static public int size = 10;

      public IndexedNames() {
         for (int i = 0; i < size; i++){
            namelist[i] = "N. A.";
            grades[i] = 'F';
         }
      }
      public string this[int index] {
         get {
            string tmp;

            if( index >= 0 && index <= size-1 ) {
               tmp = namelist[index];
            } else {
               tmp = "";
            }

            return ( tmp );
         }
         set {
            if( index >= 0 && index <= size-1 ) {
               namelist[index] = value;
            }
         }
      }

类索引器
{
私有字符串[]名称列表=新字符串[大小];
私有字符[]等级=新字符串[大小];
静态公共int size=10;
公共指数名称(){
对于(int i=0;i如果(index>=0&&index=0&&index可能是这样的:

class Indexer
{
    private string[] namelist = new string[size];
    private string[] grades = new string[size + 1]; // size +1 to indicate different 
    // size
    static public int size = 10;

    public void IndexedNames()
    {
        for (int i = 0; i < size; i++)
        {
            namelist[i] = "N. A.";
            grades[i] = "F";
        }
    }

    public string this[int i, int j]
    {
        get
        {
            string tmp;

            // we need to return first array
            if (i > 0)
            {
                tmp = namelist[i];
            }
            else
            {
                tmp = grades[i];
            }

            return (tmp);
        }
        set
        {
            if (i > 0)
            {
                namelist[i] = value;
            }
            else grades[i] = value;
        }
    }
}
类索引器
{
私有字符串[]名称列表=新字符串[大小];
私有字符串[]等级=新字符串[size+1];//size+1表示不同
//大小
静态公共int size=10;
公共无效索引名称()
{
对于(int i=0;i0)
{
tmp=名单[i];
}
其他的
{
tmp=等级[i];
}
返回(tmp);
}
设置
{
如果(i>0)
{
名称列表[i]=值;
}
其他等级[i]=价值;
}
}
}
对不起,我无能为力

虽然索引器可以重载,并且可以有多个形式参数,但不能基于同一类中的同一参数进行两种变体。这是一种语言限制(或祝福)

然而,这将引导您选择几个选项

  • 你可以使用C#7
  • 从C#7.0开始,C#支持参考返回值(参考 返回)。引用返回值允许方法返回 对变量而不是值的引用返回给调用方 然后,调用方可以选择将返回的变量视为 通过值或引用返回。调用方可以创建新的 变量本身是对返回值的引用,称为 参考本地

    课程

    public class Bobo
    {
       private int[] _ints = { 2, 3, 4, 5, 5 };
       private string[] _strings = { "asd","asdd","sdf" };
    
       public Bobo()
       {
    
          Strings = new GenericIndexer<string>(_strings);
          Ints = new GenericIndexer<int>(_ints);
       }
       public GenericIndexer<string> Strings ;
       public GenericIndexer<int> Ints ;
    
       public void Test()
       {
          _ints[0] = 234;
       }
    
       public ref int DoInts(int pos)  => ref _ints[pos];
       public ref string DoStrings(int pos)  => ref _strings[pos];
    }
    

    我认为只有两个参数的索引器才能实现您想要的

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    
    namespace ConsoleApp1
    {
        class MyClass
        {
            protected static Dictionary<string, FieldInfo[]> table = new Dictionary<string, FieldInfo[]>();
            static public int size = 10;
    
            protected char[] grades = new char[size];
    
            public object this[string name, int index]
            {
                get
                {
                    var fieldInfos = table[this.GetType().FullName];
                    return ((Array)fieldInfos.First((x) => x.Name == name).GetValue(this)).GetValue(index);
                }
                set
                {
                    var fieldInfos = table[this.GetType().FullName];
                    ((Array)fieldInfos.First((x) => x.Name == name).GetValue(this)).SetValue(value, index);
                }
            }
    
            static void Main()
            {
                var names = new MyChildClass();
                names[DataColumns.Grades, 1] = 'S';
                names[DataColumns.NameList, 9] = "W.S";
            }
        }
    
        class MyChildClass : MyClass
        {
            private string[] namelist = new string[size];
    
            static MyChildClass()
            {
                var t = typeof(MyChildClass);
                table.Add(t.FullName, t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance));
            }
    
            public MyChildClass()
            {
                for (int i = 0; i < size; i++)
                {
                    namelist[i] = "N. A.";
                    grades[i] = 'F';
                }
            }
        }
    
        static class DataColumns
        {
            public static string NameList = "namelist";
            public static string Grades = "grades";
        }
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用System.Linq;
    运用系统反思;
    名称空间控制台EAPP1
    {
    类MyClass
    {
    受保护的静态字典表=新字典();
    静态公共int size=10;
    受保护字符[]等级=新字符[大小];
    公共对象[字符串名称,int索引]
    {
    得到
    {
    var fieldInfos=table[this.GetType().FullName];
    返回((数组)fieldInfos.First((x)=>x.Name==Name).GetValue(this)).GetValue(index);
    }
    设置
    {
    var fieldInfos=table[this.GetType().FullName];
    ((数组)fieldInfos.First((x)=>x.Name==Name).GetValue(this)).SetValue(value,index);
    }
    }
    静态void Main()
    {
    var name=new MyChildClass();
    名称[DataColumns.Grades,1]=“S”;
    名称[DataColumns.NameList,9]=“W.S”;
    }
    }
    类MyChildClass:MyClass
    {
    私有字符串[]名称列表=新字符串[大小];
    静态MyChildClass()
    {
    var t=类型(MyChildClass);
    table.Add(t.FullName,t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance));
    }
    公共MyChildClass()
    {
    对于(int i=0;i
    “但我希望能够通过索引器分别访问名称列表和等级?”像
    名称.名称列表[0]
    名称.等级[0]
    我的意思是,无论什么都是合适的语句。我想你在else语句中将
    j
    拼错为
    I
    ,另外,很抱歉,我没有寻找这个,因为这很快会使代码变得不可读,谢谢你的回答。现在我正在使用引用来弥补这一点,我只是希望使代码与代码库中的其他所有类一样统一,我们有索引器,但在这一类中,我们使用引用,子类实际上不是一个选项,因为这些变量也相互作用,处理继承是我不愿意承担的任务。遗憾的是,没有直接做到这一点的方法。@anand_v.singh更新在备注中,您可以使用第二个选项来完成您描述的两项操作。这可能很有趣,让我试试;)“传递为参考”的想法在单独的部分中起作用,我会等一天,如果没有更好的解决方案,我会开始更新这种风格的代码。谢谢:)很抱歉,我没有寻找这个,因为这很快会使代码变得不可读和混乱,因为实际上有两个以上的数组,谢谢他回答说:@anand_v.singh如果你不介意为每个子类添加额外的代码,为什么不直接通过属性公开每个数组?你能解释一下你放在这里的代码吗?我想我不完全理解你在这里的建议。如果可能的话,请插入我在问题中发布的代码。@anand_v.singh对不起,我以前犯了一些大错误。现在我用一个完整的样本更新了我的答案。
    public class Bobo
    {
       private int[] _ints = { 2, 3, 4, 5, 5 };
       private string[] _strings = { "asd","asdd","sdf" };
    
       public Bobo()
       {
    
          Strings = new GenericIndexer<string>(_strings);
          Ints = new GenericIndexer<int>(_ints);
       }
       public GenericIndexer<string> Strings ;
       public GenericIndexer<int> Ints ;
    
       public void Test()
       {
          _ints[0] = 234;
       }
    
       public ref int DoInts(int pos)  => ref _ints[pos];
       public ref string DoStrings(int pos)  => ref _strings[pos];
    }
    
    var bobo = new Bobo();
    bobo.Ints[1] = 234;
    bobo.DoInts(1) = 42;
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    
    namespace ConsoleApp1
    {
        class MyClass
        {
            protected static Dictionary<string, FieldInfo[]> table = new Dictionary<string, FieldInfo[]>();
            static public int size = 10;
    
            protected char[] grades = new char[size];
    
            public object this[string name, int index]
            {
                get
                {
                    var fieldInfos = table[this.GetType().FullName];
                    return ((Array)fieldInfos.First((x) => x.Name == name).GetValue(this)).GetValue(index);
                }
                set
                {
                    var fieldInfos = table[this.GetType().FullName];
                    ((Array)fieldInfos.First((x) => x.Name == name).GetValue(this)).SetValue(value, index);
                }
            }
    
            static void Main()
            {
                var names = new MyChildClass();
                names[DataColumns.Grades, 1] = 'S';
                names[DataColumns.NameList, 9] = "W.S";
            }
        }
    
        class MyChildClass : MyClass
        {
            private string[] namelist = new string[size];
    
            static MyChildClass()
            {
                var t = typeof(MyChildClass);
                table.Add(t.FullName, t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance));
            }
    
            public MyChildClass()
            {
                for (int i = 0; i < size; i++)
                {
                    namelist[i] = "N. A.";
                    grades[i] = 'F';
                }
            }
        }
    
        static class DataColumns
        {
            public static string NameList = "namelist";
            public static string Grades = "grades";
        }
    }