Literal suffix for byte in .NET?

Matthias picture Matthias · Mar 21, 2011 · Viewed 60.6k times · Source

I am wondering if there is any way to declare a byte variable in a short way like floats or doubles? I mean like 5f and 5d. Sure I could write byte x = 5, but that's a bit inconsequential if you use var for local variables.

Answer

Matt picture Matt · Mar 21, 2011

There is no mention of a literal suffix on the MSDN reference for Byte as well as in the C# 4.0 Language Specification. The only literal suffixes in C# are for integer and real numbers as follows:

u = uint
l = long
ul = ulong
f = float
m = decimal
d = double

If you want to use var, you can always cast the byte as in var y = (byte) 5

Although not really related, in C#7, a new binary prefix was introduced 0b, which states the number is in binary format. Still there is no suffix to make it a byte though, example:

var b = 0b1010_1011_1100_1101_1110_1111; //int