Her*_*ton 4 c# visual-studio-2010
我正在研究一个名为PByte的类,它应该表示一个介于32和126之间的int值.(PByte = Printable Byte.)现在我想阻止类的用户错误地初始化一个对象,但我不想要抛出异常,我只是希望Visual Studio不编译,就像你尝试这样做时发生的那样: byte b = 256;
sealed class PByte : IEquatable<PByte>, IComparable, IComparable<PByte>
{
public PByte(int value)
{
/* if (value < 32 || value > 126)
throw new ArgumentException("\"" + value + "\" is not a valid PByte-value"); */
this._value = value;
}
[...]
Run Code Online (Sandbox Code Playgroud)
我也实现了这个:
[...]
public static implicit operator PByte(int value)
{
/* if (value < 32 || value > 126)
throw new ArgumentException("\"" + value + "\" is not a valid PByte-value"); */
return new PByte(value);
}
}
Run Code Online (Sandbox Code Playgroud)
所以这也应该是不可能的:
PByte p = 2000;