本文介绍了是否有可能整包,并调用它像整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在的IDictionary<的INT,INT,驱动程序和GT;
少一点关于它的含义晦涩,所以我想也许包裹这将是一个不错的主意。结果会是这样的的IDictionary< finishPosition,驱动方式>
I want to make the int in IDictionary<int, Driver>
a little less obscure about its meaning, so I thought maybe wrapping it would be a good idea. The outcome would be something like IDictionary<finishPosition, Driver>
.
最好,finishPosition应该分配这样的 finishPosition = 1
。
Preferably, finishPosition should be assignable like this finishPosition = 1
.
我不知道这是可能的,以及如何。
I do not know if this is possible and how.
推荐答案
您可以使用结构
与隐式转换。
You could use a struct
with an implicit conversion.
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)]
这篇关于是否有可能整包,并调用它像整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!