Should I use uint in C # for values ​​that cannot be negative?

I just tried to implement a class where numerous properties are length / count, etc. uint instead of int . However, while doing this, I noticed that it really hurts, as if no one really wants it.

Almost everything that distinguishes an integral type returns an int , so it needs to be dropped at several points. I wanted to build a StringBuffer with its default buffer length for one of the fields of this class. A cast is also required.

So, I was thinking if I just need to go back to int here. In any case, I do not use the entire range. I just thought that what I am dealing with simply cannot be negative (if that were the case, it would be a mistake), it would be a good idea to use uint .

PS: I saw this question , and this at least explains why the framework itself always uses int , but even in the native code it is actually cumbersome to stick to uint , which makes me think that this, apparently, is not really required.

+60
c # integer
Jan 6 '10 at 13:17
source share
8 answers

While strictly you should use uint for variables that contain a non-negative integer, you are faced with one of the reasons why this is not always practically possible.

In this case, I do not think that the decrease in readability that comes with the need to throw is worth it.

+35
Jan 06 '10 at 13:20
source share

I will add to other answers also that using uint as a type of public field, property, method, parameter, etc. is a violation of the rules of the Common Language Specification and should be avoided whenever possible.

+48
Jan 06 '10 at 15:19
source share

A negative value is often used to signal an error, and the size of the operation is often returned by a function call; a negative value can therefore signal an error without resorting to an exception mechanism.

Also note that .NET is often based on direct C libraries, so it’s prudent to continue this convention. If you require a larger index space, you may break the convention for different error signaling mechanisms.

+4
Jan 6 '10 at 13:22
source share

My personal feeling is that you probably should just stick with int. You should not add a cast to almost every access to a resource just to return a number range that .NET is unlikely to allow you to use anyway.

+3
Jan 06 '10 at 13:21
source share

Using int is also useful for detecting integer overflows in operations.

+3
Jan 06
source share

IMO, the disadvantage of using uint is that it hides the error conditions. The equivalents of the following code are not so good:

 if (len < 0) terminate_program("length attained impossible value."); 

Of course, your programs should not calculate anything to begin with, but I believe that they should also be written to quickly detect numeric eras without spreading. In the case where MaxValue 2 ^ 31 is enough, I say to use int along with the correct use of System.Diagnostics.Debug.Assert() and the corresponding error checks, as shown above.

If you use uint , use it together with checked to prevent overflow and get the same results. However, I found that validation is a bit complicated to apply to existing code that uses castes for any purpose.

+3
Jun 03 '10 at 3:13
source share

If you want to verify that the value is positive, the best way is to simply use assert (note that this is just a debugging method - you have to make sure that this never happens in the final code).

 using System.Diagnostics; ... Debug.Assert (i > 0); 
+1
Jan 06 '10 at 13:32
source share

Do not swim upstream if you do not need it. Not coding your code with throws makes your code more readable. Also, if your possible values ​​match inside an int, then using int is not a problem.

If you are afraid that you might overflow int, then be sure to, but do not prematurely optimize it.

I would say that the improved readability of minimizing castings outweighs the slightly increased risk of error using int.

+1
Jun 03 '10 at 3:26
source share



All Articles