I suggest you ...

Ranged Data Types In C#

I suggest to improve current numeric types by adding a way to set a value range. This can be done by some attributes, but it will be easier to give it a syntax like this
C#:
[5,10]int i;
VB.NET:
Dim i As Integer[5,10]
The compiler must not allow I to have any value < 5 or >10.
One can omit one limit:
[,100]byte
[13,]int
This will make it easy to write auto-properties like this:
C#:
[18,30]int Age {get; set;};
VB.NET:
Property Age As Integer[18,30]

Another Syntax Suggestion:
C#:
<5,10>int i;
VB.NET:
Dim i As Integer<5,10>

36 votes
Vote
Sign in
(thinking…)
Password icon
Signed in as (Sign out)
You have left! (?) (thinking…)
eng. Mohammad Hamdy shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

6 comments

Sign in
(thinking…)
Password icon
Signed in as (Sign out)
Submitting...
  • Henry commented  ·   ·  Flag as inappropriate

    No. Build a c# class instead. Generics for properties would solve this as well.

  • eng. Mohammad Hamdy commented  ·   ·  Flag as inappropriate

    After some discussions, it appeared that there are 4 possible ways to do it:
    1. Method contracts. It is verbose but can write generic conditions.
    2. Constrained Data Types: It is a compact syntax and can be extended to use regular expressions.
    3. Validation attributes: Like those used in ASP.NET MVC. It has no effect on the c# syntax if self, and we can use parameters that indicate the error message or other actions. But using attribute for individual method parameters and return value can make the code long and ugly!
    4. Writing an analyzer, which I cannot exactly understand !

    Sure we all want some syntax to simplify validation codes.
    We can make compromises. For example:
    If the constrained types can cause problems, they can only be used to define properties and methods (parameters and return values). They are nit true types but just syntax annotations to tell compiler how validate values, so the compiler can convert then to contracts under the hood.
    Attributes also can be used with properties and methods to supply error message and additional options to tell the compiler what to do when the data are not valid.
    This approach says that contracts can still be used in more complicated situations (but I see that it is easer to use regular code statements)

  • eng. Mohammad Hamdy commented  ·   ·  Flag as inappropriate

    If you want to complicat things, I suggest to use regular expression syntax such as:
    [reg exp]int x;
    This can allow writting constrains on string data types as well.

  • eng. Mohammad Hamdy commented  ·   ·  Flag as inappropriate

    In-Range Data Types gives an easier way to achieve Method Contracts. It is very clear that this syntax
    ```
    public <0,Count>int Insert(T item, <0,Count>int index) {
    return InsertCore(item, index);
    }
    ```
    is easear than:
    ```
    public int Insert(T item, int index)
    requires index >= 0 && index <= Count
    ensures return >= 0 && return < Count
    {
    return InsertCore(item, index);
    }
    ```
    If you constrained the data type, then you've already constrained all other language elements built with it.

  • eng. Mohammad Hamdy commented  ·   ·  Flag as inappropriate

    I don't ask for numeric range array . I want a limited value range variable. that can have a single value granteed to be in the range. This is a sort of value validation, that can be aplied to Properties and will affect binding them to data entry controls.

  • eng. Mohammad Hamdy commented  ·   ·  Flag as inappropriate

    This can be done by adding two readonly properties to each data type: Min, Max.. The compiler should check them before assigning any value to the object.
    Max and Min Values can also be set via the construcor.

Feedback and Knowledge Base