Should I use the types ushort, uint and ulong whenever the number is equal to or greater than 0?

9
Is it a good practice or is there something from Microsoft recommending the use of ushort , uint and ulong whenever I am sure the value will be equal to or greater than 0?

Can I gain some advantage by using them instead of the traditional short , int and long ? (In addition to being unable to receive a number less than 0).

An example would be a method that would generate a random user name, and you could enter the maximum length:

public static string GenerateUsername(int maxLength) {...}

In this case I always know that the maximum length will not be less than 0, so I should use uint instead of int ?

My doubt arose mainly because I believe I have never seen the use of these types in frameworks , libraries, etc ...

    
asked by anonymous 19.03.2018 / 00:10

1 answer

11

No, these types exist primarily for low-level communication with the operating system or other services that require these types.

Framework , except when doing the above, is all designed to work with flagged types.

Unsigned types really can not have a negative number stored in it, but nothing guarantees that a negative number is assigned to it, which will cause a loss of data and will not have the expected result (in context unchecked ", C # protects against misuse by default). Do not even think of these types as positive natural numbers, they are not this.

A username with 4 billion characters? Hope not. A byte is sufficient, and this type is signaled because almost always when it only wants 1 byte of capacity, or it integer. Some people complain about this language decision.

If it were another example that makes sense, int would allow 2 billion in size. Most often this is sufficient, so much so that .NET collections use int as length . If it needs more than 2 billion, it probably needs well over 4 billion, and then a long is more appropriate. It is very rare to need more than 2 billion and less than 4. If this is required, long is still the best solution. The loss of 4 bytes is not usually a problem. If it's a problem then uint can be used, but be very careful and prepare to have other problems communicating with the rest of the application that does not expect this type.

Just because it does not allow value less than 0 is not a reason to use uint .

If you use uint alone does not have as much of a problem, the broth goes down when you start mixing it with the flagged type, the promotion rules start to get complicated.

In C it's much more used because it's a language that does more low-level stuff. Although the use ends up being worth even more in fixed size types since the standard of the C is to have types with size dependent of the platform. In C # the usage is extra rare, and almost every few cases is because of interoperability.

This is not the case with C # today, but it has languages that make "smart" use in certain negative number situations where only positive ones are expected.

The rules of one language are not the same as those of another, which complicates a little more.

As incredible as it seems to handle a unsigned is usually slower in major processor architectures, just as short may be slower than int .

While today saving memory is more important, people talk silly when they think that memory is no problem nowadays. The typical processor today has only 4, 6 or 8MB, in mobile even that, and it is a medium slow memory, the fast one has 32 or 64KB. RAM is an aid to storing large amounts of data that does not fit in the processor, to make something be fast the data must be in the processor, this gives much more performance than saving some pure processing cycles.

But do not try to make savings without testing very well in various scenarios, and only if you need too much, and know all the details that can change. That is, it is the kind of optimization that is not for the spout of almost any programmer.

Before you see the example on Fiddle note down the results you expect this code to print:

var x = 0u;
System.Console.WriteLine(x - 1);

See in .NET Fiddle .

Have you hit all the results? Are you sure it will always hit everyone? You know all the rules? neither do I know:)

Try passing% w / w% where% w / w% is expected. Is there a solution, but is it ideal or does it seem like gambiarra by a wrong decision before?

These types are not even part of CLS .

If you want a type that guarantees only positives write a one that does this and that interacts well with all other numeric types, including those that do not seem to be so numeric like int .

That's why I always say that programming is more complicated than it sounds, and that's fine, it would not be fun if it were not :) Luckily, some people try to learn all the details. By bad luck of all the majority considers that only learning the superficial is sufficient.

It's great to have the type in the language for cases where it's needed, but it's not to use without reason.

    
19.03.2018 / 01:28