I am working on a PByte class that should represent an int value between 32 and 126. (PByte = Printable Byte.) Now I want the class user to not initialize the object incorrectly, but I do not want to throw an exception, I just want Visual Studio not to compile , as it happens when trying to do this: byte b = 256;
sealed class PByte : IEquatable<PByte>, IComparable, IComparable<PByte> { public PByte(int value) { this._value = value; } [...]
I also implemented this:
[...] public static implicit operator PByte(int value) { return new PByte(value); } }
So this should also be impossible: PByte p = 2000;
source share