c# – Which is better to use uint or int as a function argument if the value should be> = 0. What to use as an identifier?


I don't see uint , ushort , ulong anywhere in someone else's C # code. If a strictly positive argument is required, then at the beginning of the function there is usually a check that throws an exception if the argument is negative. Although the argument could be positive if uint used in the functions.

Why isn't it used?

Why don't uint be used as the primary key in the database table if the ID is still positive (I haven't seen negative IDs in any of the projects)?

Is there any reason to use regular int instead of uint ( unsigned int )?


The point is that the assembly that is obtained during compilation can be used by other languages ​​that run on the .NET platform. These languages ​​may or may not have unsigned integers, and if public assembly methods contain types like uint , your assembly cannot be used with those languages. (If an assembly can work with all languages ​​under .NET, it is said to be CLS-compliant.)

If you are writing a library for "the whole world" then using it from exotic languages ​​is not a bad goal, so such libraries are often type-limited. If you are writing code for yourself, or you are not writing a library, then in theory you can use any suitable type.

If you want your assembly to be compatible with other languages, the easiest way is to give it the [assembly: CLSCompliant(true)] attribute (written in any file, outside of classes, for example, in Assembly.cs ), and the compiler will warn you about violations. Detailed documentation here .

Scroll to Top