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

2007/02/27

Tags: C#

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:

Otherwise:

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 powered by Disqus Updated: 2019-01-23 11:41:32 +0100 +0100, Version: 6668e21