我想让IDictionary<int, Driver>中的int对其含义的理解不那么模糊,所以我认为也许包装它是一个好主意。结果将类似于IDictionary<finishPosition, Driver>

最好,finishPosition应该像finishPosition = 1这样可分配的。

我不知道这是否可行以及如何实现。

最佳答案

您可以使用带有隐式转换的struct

public struct FinishPosition {
    public readonly int Position;

    public FinishPosition(int position) {
        this.Position = position;
    }

    public static implicit operator FinishPosition(int position) {
        return new FinishPosition(position);
    }
}


// ...

Dictionary<FinishPosition, Driver> dict = new Dictionary<FinishPosition, Driver>();
Driver bob = new Driver();

// The following two lines are effectively equivalent
dict[new FinishPosition(7)] = bob;
dict[7] = bob;

// So are these two
bob = dict[7];
bob = dict[new FinishPosition(7)]

10-08 05:07