C# Constants

Constants are fixed values given to a variable, that our program may not alter during the program execution.

Integer Constants in C#

An integer constant can be a decimal, octal, or hexadecimal constant. Here are the prefix that specifies the base:

  • 0x or 0X for hexadecimal
  • 0 for octal

Here are the list of some legal integer constants C#:

214	// decimal integer constant
0xFeeL	// hexadecimal integer constant
0324	// octal integer constant

Floating-point Constants in C#

A floating-point constants in C# consists of the following parts:

  • integer part
  • decimal part
  • fractional part
  • exponent part

Here are the list of some legal floating-point constants in C#:


Character Constants in C#

Character constants are enclosed in single quotes like 'a', 'b', 'c', 'm', etc. There are some characters in C#, preceded by a backslash. They have special meaning like '\n' for newline, '\t' for tab, etc. Here the following table lists some escape sequence codes in C#:

Escape Sequence Code Output
\\ \
\? ?
\' '
\" "
\t Horizontal tab
\v Vertical tab
\n Newline
\r Carriage return
\a Alert
\f Form feed
\b Backspace
\ooo Octal number of 1 to 3 digits
\xhh . . . Hexadecimal number of one/more digit(s)

Here is an example showing some escape sequence code in C#

/* C# Constants - Example Program */
using System;
namespace EscapeSequenceChars
   class Escapeclass
      static void Main(string[] args)

When the above code is compiled and executed, it would produce the following output:


I	am	"C#"

String Constants in C#

String constants are enclose in double quotes. Here are the list of some legal string constants in C# programming:

"Hello World"
"this is C# constants tutorial"
"you are learning C# constants at codescracker.com"

C# Online Test

« Previous Tutorial Next Tutorial »

Like/Share Us on Facebook 😋