c#-运算符-自定义索引运算符

using System;
using System.Collections.Generic;
using System.Linq;

//c#-运算符-自定义索引运算符
namespace ConsoleApp11
{
    class A {
        public A(int b) {
            B = b;
        }
        public int B { get; }
    }
    class ACollection {
        public ACollection(params A[] alist) => this.alist = alist.ToArray();
        private A[] alist;
        public A this[int index] {
            get => alist[index];
            set => alist[index] = value;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            ACollection al = new ACollection(new A(1), new A(2), new A(3));
            for (int i = 0; i < 3; i++) {
                Console.WriteLine(((A)al[i]).B);
            }
        }
    }
}

c#-运算符-自定义索引运算符_第1张图片

你可能感兴趣的:(c#-函数式编程)