C# Const | C# Constants - c# - c# tutorial - c# net
How to define constants in C# ?
- In constants, the const keyword to declare a constant field or a constant local.
- Constant fields and locals be located variables and may not be modified.
- Constants can be numbers, Boolean values, strings, or a null reference.
- Don’t generate a constant to represent information that we expect to change at any time.
- The constants are treated just like regular variables except that their values cannot be modified after their definition.
Types of Constant:
- Integer constants
- Floating-point constants
- Character Constants
- String constants
- An "integer constant" is a decimal (base 10), octal (base 8), or hexadecimal (base 16) number that represents an integral value.
- Integer constants are used to represent integer values that cannot be changed
- A floating-point has an integer part, a decimal point, a fractional part, and an exponent part.
- Character constant are enclosed in single quotes ( ‘ ’ ). For example, 'w' and it can be stored in a simple variable of char type.
|\n||New line Formation|
|\a||Alert or bell|
|\ooo||Octal number of one to 3 digits|
|\xhh . . .||Hexadecimal number of one or more digits|
- A string contains characters that are similar to character constants:
- Plain characters,
- Escape sequences, and
- Universal characters.
- In this example const int SIDE =10 is a constant integer data.
- int area is specified as integer data type.
- In this example, here we are using the variable name as area, and area=SIDE*SIDE its store the constant value of 10.
- In this example, "The area of the square with SIDE" is represented as a console statement. SIDE is constant integer variable. area is specified as integer data type.
Sample C# examples - Output :
- Here in this output "The area of the square with SIDE" is represented as a statement. And 10 is specifies the integer variable, 100 is specifies the area square of integer value 100.