【问题标题】:Implement a function as []将函数实现为 []
【发布时间】:2010-11-08 22:27:02
【问题描述】:

我有一个数组,它确实是一个函数,但是我想将它用作一个数组。我知道我可以写这些

int var { get{return v2;} }
public int this[int v] { get { return realArray[v]; }

但是我如何实现一个像数组一样的函数呢?我想做类似的事情

public int pal[int i] { get { return i*2; } }

但是会出现编译错误

error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)

【问题讨论】:

    标签: c# arrays properties


    【解决方案1】:

    在 C# 中,声明参数化属性的唯一可能方法是索引器。但是,您可以通过创建一个提供索引器的类并将该类型的属性添加到您的类来模拟类似的事情:

    class ParameterizedProperty<TProperty, TIndex> {
         private Func<TIndex, TProperty> getter;
         private Action<TIndex, TProperty> setter;
         public ParameterizedProperty(Func<TIndex, TProperty> getter,
                                      Action<TIndex, TProperty> setter) {
            this.getter = getter;
            this.setter = setter;
         }
         public TProperty this[TIndex index] {
            get { return getter(index); }
            set { setter(index, value); }
         }   
    }
    
    class MyType {
        public MyType() {
            Prop = new ParameterizedProperty<string, int>(getProp, setProp);
        }
        public ParameterizedProperty<string, int> Prop { get; private set; }
        private string getProp(int index) {
            // return the stuff
        }
        private void setProp(int index, string value) {
            // set the stuff
        }
    }
    
    MyType test = new MyType();
    test.Prop[0] = "Hello";
    string x = test.Prop[0];
    

    您可以通过适当地从类中删除 getter 或 setter 来扩展只读和只写属性的想法。

    【讨论】:

      【解决方案2】:

      正如您所注意到的,您不能这样命名索引器,因此:

      public int this[int i] { get { return i * 2; } }
      

      或者,如果您真的打算将其命名为pal

      public class Wrapper
      {
          public int this[int i] { get { return i * 2; } }
      }
      
      ...
      
      public Wrapper pal { get { return _someWrapperInstance; } }
      

      然后可以访问pal[ix]pal[3]等。

      【讨论】:

        【解决方案3】:

        要么你返回一个数组对象:

        public int[] pal { get { return realArray; } }
        

        或者您返回一个具有索引器的对象:

        public class ActingAsArray {
           private int[] _arr;
           public ActingAsArray(int[] arr) { _arr = arr; }
           public int this[int v] { get { return _arr[v]; } }
        }
        
        public ActingAsArray pal { get { return new ActingAsArray(realArray); } }
        

        【讨论】:

          【解决方案4】:

          您不能在 C# 中重载 (overloadable operators) 括号运算符。正如您所展示的,您能做的最好的事情就是实现indexer。根据文档,您必须使用 this 关键字来实现索引器。索引器的工作方式与属性非常相似,它们有一个 getter 和一个 setter,您可以在 getter 或 setter 中执行几乎任何功能。

          【讨论】:

            【解决方案5】:

            如果您不介意使用一点 VB.Net,它支持参数化属性(仍然让我感到惊讶,为什么在 C# 中不可能,因为 .Net 显然能够做到)

            这样您就可以在 VB.Net 中创建您的类并在您的项目中引用 VB.Net DLL。

            如果你的班级经常变化,这当然会有点烦人:-/

            【讨论】:

              猜你喜欢
              • 2021-04-28
              • 1970-01-01
              • 2017-07-02
              • 2022-01-10
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多