开发者

Is it possible to create a new Type in C#?

开发者 https://www.devze.com 2023-04-11 21:35 出处:网络
I want to create a new type, i. e: [sbyte(1 Byte) Value-range :-128 to +127] [byte(1 Byte) Value-range :0 to +255]

I want to create a new type, i. e:

[sbyte  (1 Byte) Value-range :        -128 to +127]
[byte   (1 Byte) Value-range :           0 to +255]    
[short  (2 Byte) Value-range :      -32768 to +32767]
[ushort (2 Byte) Value-range :           0 to +65535]    
[int    (4 Byte) Value-range : -2147483648 to +2147483647]
[uint   (4 Byte) Value-range :           0 to +4294967295]
.
.
.
etc.

now I want to create LInt(LowInt) Type in range 0 to 200000, s开发者_如何学Co I dont want to use uint or int.

Is it any possible with C#?

Thank You!


Yes, that is what classes are for. Now, you cannot create a new "builtin" type. The semantics may not be exactly the same, but I'm guessing you won't care too much about those differences.


It is not possible in a built-in way, using simple language, e.g. int<5000,20000>.

While it is possible with a more complex definition, it isn't easy. It is difficult to determine and define all the semantics you'd want, such as:

  • Conversion to other ranged types
  • Conversion from built in numeric types
  • What should happen on overflow/underflow
  • What should the resulting type's range be after arithmetic is performed

Such a solution also wouldn't cover cases where you'd want one parameter's type range be constrained based on another parameter's value.

An alternative would be to use something like Code Contracts to define contracts for your code so you can do arbitrary static (compile time) verifications.

Edit: Removed link to SpecSharp. It looks like it was replaced with Code Contracts...


you can also use a struct to create a "type", but you are not able to create a primitive type.


You can create an object and handle your conditions, if you want it to act more like a primitive try making it a struct


It is of no use. Your CPU architecture defines what chunks of data it can handle at once and for what purposes. Even if you created a type that only allowed for different min-max values (which you can implement in a class and operator overloading), it would not be more efficient in neither memory nor performance aspects.

If you want to simply enforce some semantics, here's something that should lead in you the right direction (it's by no means complete, and lacks support for the checked and unchecked keywords).

struct MyInt
{
    static uint max = 200000;
    readonly uint i;
    public MyInt(uint i) {
        this.i = i;
    }

    public static implicit operator MyInt(uint j) {
        if(j > max)
            throw new ArgumentException();
        return new MyInt(j);
    }

    public static MyInt operator +(MyInt i, uint j) {
        try {
            return new MyInt(i.i + j);
        }
        catch(ArgumentException) {
            throw new OverflowException();
        }
    }

    public override string ToString ()
    {
        return i.ToString();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号