C# - Variables
In C#, variables are used to store data that can be manipulated and retrieved throughout a program. They are essential for holding values, performing operations, and managing program state. Here’s a comprehensive guide to variables in C#:
1. Declaring Variables
To declare a variable in C#, specify the data type followed by the variable name. Optionally, you can initialize the variable with a value.
Syntax:
csharpdataType variableName; // Declaration
dataType variableName = initialValue; // Declaration with initialization
Examples:
csharpint age; // Declaration
age = 25; // Initialization
double price = 19.99; // Declaration with initialization
2. Data Types
Variables in C# are strongly typed, meaning each variable must be declared with a specific data type. The data type determines what kind of data the variable can hold.
Basic Data Types:
Integer Types:
int
- 32-bit signed integerlong
- 64-bit signed integershort
- 16-bit signed integerbyte
- 8-bit unsigned integer
Floating-Point Types:
float
- 32-bit floating-pointdouble
- 64-bit floating-point
Character and String Types:
char
- 16-bit Unicode characterstring
- Sequence of characters
Boolean Type:
bool
- Representstrue
orfalse
Example:
csharpint number = 42;
double temperature = 98.6;
char letter = 'A';
string name = "Alice";
bool isActive = true;
3. Variable Initialization
Variables can be initialized at the time of declaration or later in the code.
Declaration and Initialization:
csharpint count = 0;
string greeting = "Hello, World!";
Initialization Later:
csharpint count;
count = 10; // Initialization after declaration
4. Constants
Constants are variables whose values cannot be changed once they are assigned. They are declared using the const
keyword.
Syntax:
csharpconst dataType constantName = value;
Example:
csharpconst double Pi = 3.14159;
const int MaxUsers = 100;
5. Variable Scope
The scope of a variable determines where it can be accessed within the code. C# variables have different scopes based on where they are declared.
Local Variables: Declared inside methods, constructors, or blocks. Accessible only within those methods or blocks.
csharpvoid SomeMethod() { int localVar = 10; // Local variable }
Instance Variables: Declared within a class but outside of methods. They are accessed by instances of the class.
csharpclass MyClass { public int instanceVar; // Instance variable }
Static Variables: Declared with the
static
keyword. They are shared among all instances of a class.csharpclass MyClass { public static int staticVar; // Static variable }
6. Variable Naming Conventions
- Camel Case: For local variables and parameters, start with a lowercase letter and use uppercase for subsequent words (e.g.,
totalAmount
). - Pascal Case: For fields, properties, and constants, start with an uppercase letter and use uppercase for subsequent words (e.g.,
MaxValue
).
7. Var Keyword
C# allows you to use the var
keyword for implicit type inference, where the compiler determines the type of the variable based on the assigned value.
Syntax:
csharpvar variableName = initialValue; // Type is inferred from the value
Example:
csharpvar number = 10; // int
var name = "Alice"; // string
var price = 19.99; // double
8. Nullable Types
C# provides the ability to use nullable types for value types, which can represent all the values of its underlying type plus an additional null
value.
Syntax:
csharpint? nullableInt = null; // Nullable int
Summary
Variables in C# are fundamental to storing and manipulating data. They must be declared with a specific data type, and their scope determines where they can be accessed. C# also supports constants, implicit type inference with var
, and nullable types to enhance flexibility. Understanding how to properly use and manage variables is crucial for effective programming in C#.