C# is a powerful and versatile language, and understanding its basic syntax is the first step towards mastering it. This lesson will cover the fundamental building blocks of C# code: variables, data types, and operators. These concepts are essential for writing any C# program, from simple console applications to complex web applications. By the end of this lesson, you'll have a solid foundation for building more advanced C# applications.
Variables are named storage locations in a computer's memory that hold data. Think of them as labeled boxes where you can store different types of information. In C#, you must declare a variable before you can use it. This declaration involves specifying the variable's data type and its name.
The syntax for declaring a variable in C# is:
data_type variable_name;
For example:
int age; // Declares an integer variable named 'age'
string name; // Declares a string variable named 'name'
double salary; // Declares a double-precision floating-point variable named 'salary'
int, string, and double are examples of data types, which we'll discuss in detail later.age, name, and salary are the names you choose for your variables. Variable names should be descriptive and follow certain rules:
_).age and Age are different variables).int, string, class, etc.).After declaring a variable, you can assign it a value. This is called initialization. You can initialize a variable at the time of declaration or later in your code.
int age = 30; // Declares an integer variable 'age' and initializes it to 30
string name = "John Doe"; // Declares a string variable 'name' and initializes it to "John Doe"
double salary; // Declares a double variable 'salary'
salary = 50000.00; // Initializes 'salary' to 50000.00
It's good practice to initialize variables when you declare them to avoid unexpected behavior. If you don't initialize a variable, C# assigns it a default value (e.g., 0 for numeric types, null for reference types like string).
The scope of a variable determines where in your code you can access it. Variables declared inside a block of code (e.g., within curly braces {}) are only accessible within that block. This is known as block scope.
void MyMethod()
{
int x = 10; // 'x' is accessible within MyMethod
if (true)
{
int y = 20; // 'y' is accessible only within this 'if' block
Console.WriteLine(x); // Valid: 'x' is accessible here
}
// Console.WriteLine(y); // Invalid: 'y' is not accessible here (out of scope)
Console.WriteLine(x); // Valid: 'x' is accessible here
}
Variables declared outside any block (e.g., at the class level) have class scope and are accessible throughout the class. We'll explore class-level variables in more detail when we discuss classes in Module 2.
A data type specifies the kind of values a variable can hold. C# is a strongly-typed language, which means that every variable must have a declared data type, and the compiler enforces type safety. C# provides a rich set of built-in data types, which can be broadly categorized into: