基本思想:
假设您希望实现以下行为:
List<CheckedInt> myIntList = new List<CheckedInt>();
CheckedInt check1 = int.MaxValue;
CheckedInt check2 = 1;
myIntList.Add(check1 + check2); //exception occurs!
将文本从英语翻译成中文:
最清晰的方法之一是这样做(使操作代码(例如x + y
)可以保留,但同时能够抛出异常
),即基于int
定义自己的CheckedInt
(带有重载运算符)。
实现
结构体
CheckedInt
struct
将类似于以下内容:
public struct CheckedInt {
private int Value { get; set; }
public CheckedInt(int value)
: this() {
Value = value;
}
public static implicit operator CheckedInt(int me) {
return new CheckedInt(me);
}
public static CheckedInt operator +(CheckedInt lhs, CheckedInt rhs) {
double testResult = (double)lhs.Value + (double)rhs.Value;
if (testResult > int.MaxValue || testResult < int.MinValue)
throw new MyCheckedIntException();
return new CheckedInt(lhs.Value + rhs.Value);
}
public static CheckedInt operator -(CheckedInt lhs, CheckedInt rhs) {
double testResult = (double)lhs.Value - (double)rhs.Value;
if (testResult > int.MaxValue || testResult < int.MinValue)
throw new MyCheckedIntException();
return new CheckedInt(lhs.Value - rhs.Value);
}
public static CheckedInt operator *(CheckedInt lhs, CheckedInt rhs) {
double testResult = (double)lhs.Value * (double)rhs.Value;
if (testResult > int.MaxValue || testResult < int.MinValue)
throw new MyCheckedIntException();
return new CheckedInt(lhs.Value * rhs.Value);
}
public static CheckedInt operator /(CheckedInt lhs, CheckedInt rhs) {
double testResult = (double)lhs.Value / (double)rhs.Value;
if (testResult > int.MaxValue || testResult < int.MinValue)
throw new MyCheckedIntException();
return new CheckedInt(lhs.Value / rhs.Value);
}
public override string ToString() {
return Value.ToString();
}
public bool Equals(CheckedInt otherInt) {
return Value == otherInt.Value;
}
}
异常
您也可以定义自己的异常。
public class MyCheckedIntException : Exception {
public MyCheckedIntException() {
}
public MyCheckedIntException(string message) : base(message) {
}
public MyCheckedIntException(string message, Exception inner) : base(message, inner) {
}
现在,你拥有了一个真正的
CheckedInt
列表。
用法
只需像这样使用:
CheckedInt check1 = int.MaxValue;
CheckedInt check2 = 1;
这个声明是:“”
List<CheckedInt> myIntList = new List<CheckedInt>()
myIntList.Add(check1 + check2)
会抛出一个名为
MyCheckedIntException
的异常。
扩展,更清晰的外观
如果你想要像这些一样使用它:
myIntList.Add(check1 + 1); //note that `1` is not type of checked integer
myIntList.Add(1 + check1); //note that `1` is not type of checked integer
然后只需将
overloading
添加到
operator overloads
中:
public static CheckedInt operator +(CheckedInt lhs, int rhs) {
double testResult = (double)lhs.Value + (double)rhs;
if (testResult > int.MaxValue || testResult < int.MinValue)
throw new MyCheckedIntException();
return new CheckedInt(lhs.Value + rhs);
}
public static CheckedInt operator +(int lhs, CheckedInt rhs) {
double testResult = (double)lhs + (double)rhs.Value;
if (testResult > int.MaxValue || testResult < int.MinValue)
throw new MyCheckedIntException();
return new CheckedInt(lhs + rhs.Value);
}
你可以同样地对所有其他运算符进行操作。
Int64
吗? - tchelidzechecked{}
。 - sujith karivelilList<>
如何检查它的情况呢? - Patryk Spytkowski