New type in C # with development time checking

Based on this topic.

Define a "custom" integer type?

The answer above is wonderful, but how can I get an error message in designtime / compiletime and not just at runtime.

If i say

byte b = 300; //This will not compile.

So how can I get the same behavior for

PackedValue p = 5000;

or in this case because of the explicit

PackedValue p = (PackedValue)5000;
+4
source share
1 answer

It's complicated, but you can look in Code Contracts , an extension for Visual Studio that allows you to specify custom compile-time checks for your code.

Using this, you can include strings in operator declarations (both implicit and explicit), such as:

Contract.Requires(val < (1<<12));

, . .

+5

Source: https://habr.com/ru/post/1534647/


All Articles