Unlocking the Power of Variables in C#
Variables are the backbone of any programming language, and C# is no exception. In this comprehensive guide, we’ll delve into the world of variables in C#, covering everything from declaration to best practices.
Declaring Variables in C#
A variable is a symbolic name given to a memory location, allowing you to store and manipulate data in your program. To declare a variable in C#, you need to specify its data type and name. For example:
int age;
Here, age
is a variable of type int
, which can store integer values.
Implicitly Typed Variables
C# also allows you to declare variables without specifying their data type using the var
keyword. This is known as implicitly typed variables. The compiler determines the data type of the variable based on the assigned value. For example:
var name = "John";
In this case, the compiler infers that name
is a string variable.
Rules for Naming Variables in C#
When naming variables, there are certain rules to follow:
- Variable names can contain letters (uppercase and lowercase), underscores, and digits.
- Variable names must start with a letter, underscore, or the @ symbol.
- C# is case-sensitive, meaning
age
andAge
refer to different variables. - Variable names cannot be C# keywords.
Best Practices for Naming Variables
To make your code more readable and maintainable, follow these best practices:
- Choose meaningful variable names that indicate their purpose.
- Use camelCase notation for local variables and PascalCase for public member variables.
- Use a leading underscore followed by camelCase notation for private member variables.
C# Primitive Data Types
C# has several primitive data types, including:
Boolean (bool)
- Represents a true or false value
- Default value: false
Signed Integral
sbyte
: 8-bit signed integer, range -128 to 127short
: 16-bit signed integer, range -32,768 to 32,767int
: 32-bit signed integer, range -2,147,483,648 to 2,147,483,647long
: 64-bit signed integer, range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Unsigned Integral
byte
: 8-bit unsigned integer, range 0 to 255ushort
: 16-bit unsigned integer, range 0 to 65,535uint
: 32-bit unsigned integer, range 0 to 4,294,967,295ulong
: 64-bit unsigned integer, range 0 to 18,446,744,073,709,551,615
Floating Point
float
: 32-bit floating-point number, range 1.5 × 10^(-45) to 3.4 × 10^38double
: 64-bit floating-point number, range 5.0 × 10^(-324) to 1.7 × 10^308
Character (char)
- Represents a 16-bit Unicode character
- Default value: ‘\0’
- Range: U+0000 (‘\u0000’) to U+FFFF (‘\uffff’)
Decimal
- 128-bit decimal number, range (-7.9 × 10^28 to 7.9 × 10^28) / (10^0 to 10^28)
- Default value: 0.0M
C# Literals
Literals are fixed values that appear in the program. They do not require any computation.
Boolean Literals
true
andfalse
are the available boolean literals.
Integer Literals
- Integer literals can be decimal, hexadecimal, or octal.
- If an integer literal ends with L or l, it is of type long.
Floating-Point Literals
- Floating-point literals can be decimal or hexadecimal.
- If a floating-point literal ends with a suffix f or F, it is of type float. If it ends with d or D, it is of type double.
Character and String Literals
- Character literals are enclosed in single quotes.
- String literals are enclosed in double quotes.
- C# also supports escape sequence characters such as \’, \”, \, \n, \r, \t, and \a.