比如说我有如下的结构:  struct Vector
  {
     private double x,y,z;   
  }请问如何让此结构的实例使用[]运算符啊要求
   Vector v1;
   v1[0]=x;
   v1[1]=y;
   v2[2]=z;

解决方案 »

  1.   

    public type this[arglist]{
        get{
          ...
          return ....
        }
        set{
          ...
        }
    }
      

  2.   

    eg:
        struct MyStruct
        {
            private Int32[] arr;
            public MyStruct(Int32 i)
            {
                arr = new Int32[i];
            }
            public Int32 this[Int32 index]
            {
                get {
                    if (index > 0 && index < arr.Length)
                        return arr[index];
                    else
                        throw new Exception("out of range");
                }
            }
        }
    public class Program
    {
            static void Main(string[] args)
            {            MyStruct myStruct = new MyStruct(10);
                Console.WriteLine(myStruct[5]);
             }
    }