YouTip LogoYouTip

Csharp Constants

# C# Constants Constants are fixed values that do not change during program execution. Constants can be of any basic data type, such as integer constants, floating-point constants, character constants, or string constants, as well as enumeration constants. Constants can be treated as regular variables, except that their values cannot be modified after definition. ## Integer Constants Integer constants can be decimal, octal, or hexadecimal constants. The prefix specifies the base: 0x or 0X denotes hexadecimal, 0 denotes octal, and no prefix denotes decimal. Integer constants can also have suffixes, which can be a combination of U and L, where U and L represent unsigned and long, respectively. Suffixes can be uppercase or lowercase, and multiple suffixes can be combined in any order. Here are some examples of integer constants: 212 /* Legal */215u /* Legal */0xFeeL /* Legal */078 /* Illegal: 8 is not an octal digit */032UU /* Illegal: cannot repeat suffix */ Here are examples of integer constants of various types: 85 /* Decimal */0213 /* Octal */0x4b /* Hexadecimal */30 /* int */30u /* unsigned int */30l /* long */30ul /* unsigned long */ ## Floating-Point Constants A floating-point constant consists of an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating-point constants in decimal form or exponential form. Here are some examples of floating-point constants: 3.14159 /* Legal */314159E-5L /* Legal */510E /* Illegal: incomplete exponent */210f /* Illegal: no decimal or exponent */.e55 /* Illegal: missing integer or fractional part */ When using decimal form, you must include the decimal point, the exponent, or both. When using exponential form, you must include the integer part, the fractional part, or both. The signed exponent is denoted by e or E. ## Character Constants Character constants are enclosed in single quotes, for example, 'x', and can be stored in a simple character type variable. A character constant can be an ordinary character (e.g., 'x'), an escape sequence (e.g., 't'), or a universal character (e.g., 'u02C0'). In C#, certain characters have special meanings when preceded by a backslash and can be used to represent a newline (n) or a tab (t). Here are some escape sequence codes: | Escape Sequence | Meaning | | --- | --- | | | character | | ' | ' character | | " | " character | | ? | ? character | | a | Alert or bell | | b | Backspace | | f | Form feed | | n | Newline | | r | Carriage return | | t | Horizontal tab | | v | Vertical tab | | ooo | One to three-digit octal number | | xhh . . . | One or more-digit hexadecimal number | Here are some examples of escape sequence characters: namespace EscapeChar{ class Program { static void Main(string[] args) { Console.WriteLine("HellotWorldnn"); Console.ReadLine(); } }} When the above code is compiled and executed, it produces the following result: Hello World ## String Constants String constants are enclosed in double quotes "" or in @"". String constants contain characters similar to character constants, which can be: ordinary characters, escape sequences, and universal characters. When using string constants, you can split a long line into multiple lines and use spaces to separate the parts. Here are some examples of string constants. The various forms listed below represent the same string. string a = "hello, world"; // hello, worldstring b = @"hello, world"; // hello, worldstring c = "hello t world"; // hello worldstring d = @"hello t world"; // hello t worldstring e = "Joe said "Hello" to me"; // Joe said "Hello" to mestring f = @"Joe said ""Hello"" to me"; // Joe said "Hello" to mestring g = "\serversharefile.txt"; // serversharefile.txtstring h = @"serversharefile.txt"; // serversharefile.txtstring i = "onerntwornthree";string j = @"one two three"; ## Defining Constants Constants are defined using the **const** keyword. The syntax for defining a constant is as follows: const = value; The following code demonstrates how to define and use constants in a program: ## Example using System; public class ConstTest { class SampleClass { public int x; public int y; public const int c1 =5; public const int c2 = c1 +5; public SampleClass(int p1, int p2) { x = p1; y = p2; } } static void Main() { SampleClass mC =new SampleClass(11, 22); Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y); Console.WriteLine("c1 = {0}, c2 = {1}", SampleClass.c1, SampleClass.c2); } } When the above code is compiled and executed, it produces the following result: x = 11, y = 22 c1 = 5, c2 = 10
← Csharp OperatorsCsharp Variables β†’