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

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

Luca -

☕ 1 min. read

Yesterday I found an old email in my mail box that I thought might be gen­er­ally in­ter­est­ing.

I was ask­ing the tech­ni­cal lead on the C# com­piler which al­go­rithm/​short­cut peo­ple should use to choose their number types’ among the many avail­able in the lan­guage. I was ask­ing for some­thing that works the ma­jor­ity of times, even if not al­ways. I’m sure there are other sce­nar­ios we haven’t con­sider. Anyhow, here is his al­go­rithm.

If you need frac­tions:

  • Use dec­i­mal when in­ter­me­di­ate re­sults need to be rounded to fixed pre­ci­sion - this is al­most al­ways lim­ited to cal­cu­la­tions in­volv­ing money.
  • Otherwise use dou­ble - you will get the round­ing of your cal­cu­la­tions wrong, but the ex­tra pre­ci­sion of dou­ble will en­sure that your re­sults will be good enough.
  • Only use float if you know you have a space is­sue, and you know the pre­ci­sion im­pli­ca­tions. If you don’t have a PhD in nu­meric com­pu­ta­tion you don’t qual­ify.

Otherwise:

  • Use int when­ever your val­ues can fit in an int, even for val­ues which can never be neg­a­tive. This is so that sub­trac­tion op­er­a­tions don’t get you con­fused.
  • Use long when your val­ues can’t fit in an int.

Byte, sbyte, short, ushort, uint, and ulong should only ever be used for in­terop with C code. Otherwise they’re not worth the has­sle.

12 Comments

Comments

Cory Nelson

2007-02-27T18:29:35Z

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.

Yeah, 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:05Z

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

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.

Eber Irigoyen

2007-02-27T20:59:12Z

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

.NET a 2.860 m de altura

2007-02-28T22:57:25Z

C# 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:07Z

Saw 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:24Z

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

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

Ronaldo Ferreira

2007-03-02T18:36:41Z

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.

>I have had throw away a lot of bytes.
internaly it maybe have been casted as int anyway by the compilator

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

0 Webmentions

These are webmentions via the IndieWeb and webmention.io.