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#-运算符-自定义索引运算符
最新推荐文章于 2024-04-17 00:22:25 发布