4

I am used to choosing the smallest data type needed to fully represent my values while preserving semantics. I don't use long when int is guaranteed to suffice. Same for int vs short.

But for real numbers, in C# there is the commonly used double -- and no corresponding single or float. I can still use System.Single, but I wonder why C# didn't bother to make it into a language keyword like they did with double.

In contrast, there are language keywords short, int, long, ushort, uint, and ulong.

So, is this a signal to developers that single-precision is antiquated, deprecated, or should otherwise not be used in favor of double or decimal?

(Needless to say, single-precision has the downside of less precision. That's a well-known tradeoff for smaller size, so let's not focus on that.)

edit: My apologies, I mistakenly thought that float isn't a keyword in C#. But it is, which renders this question moot.

Philip
  • 1,532
  • 12
  • 23

5 Answers5

8

The float alias represents the .NET System.Single data type so I would say it's safe to use.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
5

There is the following correspondence between C# keywords and .NET type names:

double -  System.Double
float  -  System.Single

So there's one keyword in C# for each of the two types in question.

I don't know how you got the impression that float was not a C# keyword. It certainly is.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
5

Actually, there is a float keyword for single precision floating point.

Also, don't be so sure about short or byte being a better fit than int. int is usually the best choice for integer numbers. Read more about it here.

Clint Warner
  • 1,265
  • 1
  • 9
  • 25
Pablote
  • 4,745
  • 9
  • 39
  • 46
3

As a default, any literal like 2.0 is automatically interpreted as a double unless otherwise specified. This could contribute to the consistently higher use of double than other floating-point representations. Just a side note.

As far as the absence of single goes, the float keyword translates to the System.Single type.

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
christopher
  • 26,815
  • 5
  • 55
  • 89
2

C# System.Single is aliased to float

NominSim
  • 8,447
  • 3
  • 28
  • 38