Which type should I use in C# to represent numbers?
Luca Bolognese -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.
Tags
- CSHARP
12 Comments
Comments
Cory Nelson
2007-02-27T18:29:35Zi 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.
Sean
2007-02-27T19:12:35ZYeah, but in the .NET framework unsigned types are not CLR compliant, which you might or might not be worried about.
Fabrice's weblog
2007-02-27T19:24:05ZLuca Bolognese, from the Microsoft C# team, has an interesting post that aims at providing answers to
NBC
2007-02-27T20:35:24Zbut 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.
Eber Irigoyen
2007-02-27T20:59:12Zbut this applies to the whole CTS, not just C#… right?
.NET a 2.860 m de altura
2007-02-28T22:57:25ZC# nos ofrece varios tipos de datos para representar números y, sobretodo para quienes están
There Must Be Some Mistake
2007-03-01T20:54:07ZSaw this post today describing basic guidance for when to use the different numeric data types available
Charlie Calvert's Community Bl
2007-03-02T03:23:24ZWelcome to the twenty-second Community Convergence, the March CTP issue. I’m Charlie Calvert, the C#
RSS It All
2007-03-02T03:37:00ZWelcome to the twenty-second Community Convergence, the March CTP issue. I'm Charlie Calvert, the
Ronaldo Ferreira
2007-03-02T18:36:41ZI 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.
NBC
2007-03-03T10:34:00Z>I have had throw away a lot of bytes.
internaly it maybe have been casted as int anyway by the compilator
AS
2007-03-11T07:34:06ZI 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 :) ]