Which type should I use in C# to represent numbers?


Yesterday I found an old email in my mail box that I thought might be generally interesting.

I was asking the technical lead on the C# compiler which algorithm/shortcut people should use to choose their ‘number types’ among the many available in the language. I was asking for something that works the majority of times, even if not always. I’m sure there are other scenarios we haven’t consider. Anyhow, here is his algorithm.

If you need fractions:

  • Use decimal when intermediate results need to be rounded to fixed precision – this is almost always limited to calculations involving money.
  • Otherwise use double – you will get the rounding of your calculations wrong, but the extra precision of double will ensure that your results will be good enough.
  • Only use float if you know you have a space issue, and you know the precision implications. If you don’t have a PhD in numeric computation you don’t qualify.

Otherwise:

  • Use int whenever your values can fit in an int, even for values which can never be negative. This is so that subtraction operations don’t get you confused.
  • Use long when your values can’t fit in an int.

Byte, sbyte, short, ushort, uint, and ulong should only ever be used for interop with C code. Otherwise they’re not worth the hassle.

Comments (12)

  1. Cory Nelson says:

    i really mean this in the nicest way: if you get confused by unsigned types, stop coding.  maybe the technical lead needs to have more faith in the competency of the developers he is targeting.

  2. Sean says:

    Yeah, but in the .NET framework unsigned types are not CLR compliant, which you might or might not be worried about.

  3. Luca Bolognese, from the Microsoft C# team, has an interesting post that aims at providing answers to

  4. NBC says:

    but if you use what the value means, isn’t it more  readable ?

    I mean if you have a file header

    with a uint32 fourcc then a uint16 version value, its more readable since you know what you should look for. if you use int fourcc and int version i don"t know how the bytes map in the file.

  5. but this applies to the whole CTS, not just C#… right?

  6. C# nos ofrece varios tipos de datos para representar números y, sobretodo para quienes están

  7. Saw this post today describing basic guidance for when to use the different numeric data types available

  8. Welcome to the twenty-second Community Convergence, the March CTP issue. I’m Charlie Calvert, the C#

  9. RSS It All says:

    Welcome to the twenty-second Community Convergence, the March CTP issue. I'm Charlie Calvert, the

  10. I coded these days a method that reads a binary file, but all data were represented with only two bytes (an ushort is enough). I really do not see the reason to "forget" these types. If I have coded this same method with int, I have had throw away a lot of bytes.

  11. NBC says:

    >I have had throw away a lot of bytes.

    internaly it maybe have been casted as int anyway by the compilator

  12. AS says:

    I want to write a Matrix (Math) with generics that can instance it with all numbers(float, double, int,…).

    any idea?

    I think it is imposible.

    [imposible says i am posible 🙂 ]