Welcome to MSDN Blogs Sign in | Join | Help

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.

Published Tuesday, February 27, 2007 3:09 PM by lucabol
Filed under:

Comments

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

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.

Tuesday, February 27, 2007 6:29 PM by Cory Nelson

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

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

Tuesday, February 27, 2007 7:12 PM by Sean

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

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

Tuesday, February 27, 2007 7:24 PM by Fabrice's weblog

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

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.

Tuesday, February 27, 2007 8:35 PM by NBC

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

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

Tuesday, February 27, 2007 8:59 PM by Eber Irigoyen

# Double, float, decimal, int, long, uint, short ¿cuál uso?

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

Wednesday, February 28, 2007 10:57 PM by .NET a 2.860 m de altura

# Numeric datatypes in C#

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

Thursday, March 01, 2007 8:54 PM by There Must Be Some Mistake

# Community Convergence XXII

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

Friday, March 02, 2007 3:23 AM by Charlie Calvert's Community Blog

# Community Convergence XXII

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

Friday, March 02, 2007 3:37 AM by RSS It All

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

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.

Friday, March 02, 2007 6:36 PM by Ronaldo Ferreira

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

>I have had throw away a lot of bytes.

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

Saturday, March 03, 2007 10:34 AM by NBC

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

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 :) ]

Sunday, March 11, 2007 7:34 AM by AS
New Comments to this post are disabled
 
Page view tracker