C# Literals & C# 7.0 Binary Literals and Digit Separators

Two features that were originally planned with C# 6 now seem to make it into C# 7.0: binary literals and digit separators. These are small features, but make the code easier to read.
When C# 7.0 brings some extensions to literals, it’s a good time talking about C# literals in overall.
This article not only shows the new C# 7.0 features, but C# literals in overall.

Old Typewriter

A literal is a notation to represent fixed values in source code.

Binary Literals

Let’s start with a new feature: binary literals. Using the 0b prefix, a binary literal can be defined:

byte b1 = 0b00001111;
byte b2 = 0b10101010;
ushort s1 = 0b1111000011110000;

Binary literals make it easy to work with bits.

Digit Separators

Just to help the readability of programs, the digit separator _ is available with C# 7.
Adding this separator to a previous defined short, the bits can be grouped by e.g. every 4 digits:

ushort s1 = 0b1011_1100_1011_0011;

How you do the grouping doesn’t matter for the compiler, it’s just for readability. The following code snippet uses the separator every three digits.

ushort s2 = 0b1_111_000_111_000_111;

Digit Separators are not limited to binaries. You can use this separator with other numbers as well. Here, it is used with an int passing a hexadecimal value:

int x1 = 0x44aa_abcd;

Boolean Literals

Let’s get into the other literals of C#. All these have been available before C# 7.0.
Boolean literals define the literal values true and false of type bool:

bool flag1 = true;
bool flag2 = false;

Integer Literals

Integer literals can be assigned to int, uint, long, and ulong. Possible forms of integer literals are in decimal and hexadecimal form (and with C# 7 in binary form as well).
Setting the variables n1, n3, n4, and n5, a number is directly assigned. Adding the suffix u to the number, an unsigned value is created that can be assigned to uint and ulong. The l or L suffix creates a long value and can be assigned to long and ulong. To create an unsigned long value, the ul suffix can be used.

int n1 = 1;
int n2 = 0xA;
uint n3 = 3;
long n4 = 4;
ulong n5 = 5;
uint n6 = 6u;
ulong n7 = 7u;
long n8 = 8L;
ulong n9 = 9L;
ulong n10 = 10ul;

To not confuse the l suffix with 1, it’s better to use the uppercase character L.

Real Literals

For real numbers, the suffix characters F and M can be used. Without a suffix, a double is created that can be assigned to a double type. To create a float, the F suffix is used, to create a decimal, the M suffix:

float f1 = 1.1F;
double d1 = 2.2;
decimal d2 = 3.30M;

Character Literals

Character literals are used to create single characters. Using single quotes, a character can be assigned to a variable of type char. Escape characters can be set starting with the backslash. Here, a predefined short notation list exists to specify special characters, such as \t for the horizontal tab. To specify any character using hexadecimal format, you can use \x before setting the character code in hexadecimal format. To pass a unicode character, \u can be used:

char c1 = 'a';
char tab = '\t'; // Tab
char hex = '\x05c';  // \
char unicode = '\u0066'; // f

Escape Characters

The short notation of escape characters available with C# are listed in the following table.

Short notation Description
\’ single quote
\” double quote
\ backslash
\a alarm (bell)
\b backspace
\f form-feed
\n new-line
\r carriage-return
\t horizontal tab
\v vertical tab

String Literals

String literals are written within double quotes. You can use escape characters here as well – like the short notation for a tab, \t as shown with the variable s2. Prefixing the string with the character @ creates a verbatim string. This way, escape characters are not interpreted. With the string s3, \t is in the string, and not a horizontal tab. With a verbatim string to add a double quote to the string, it is escaped with two double quotes.

string s1 = "Hello!";
string s2 = "Hello\tWorld";
string s3 = @"Hello\tWorld";
string s4 = "enter a \"string\"";
string s5 = @"enter a ""string""";

Null Literal

The last literal of this article is the simple null literal:

object o = null;

With many objects, if null would not be allowed, programming errors could be reduced. A proposed feature of C# Nullable reference types and nullability checking looks great in this regard. I’m just not sure when this feature makes it into C#.

Sample Code

The sample code is available at GitHub.

To run the sample code you need Visual Studio “15” Preview.

Have fun with programming and learning!

More Information

More information about C# is available in my new book and my C# workshops:

Professional C# 6 and .NET Core 1.0

Christian Nagel’s Workshops

Image from © Janaka Dharmasena | Dreamstime.com Typewriter

One thought on “C# Literals & C# 7.0 Binary Literals and Digit Separators

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s