In C#, variables are used to store and manipulate data. Each variable has a specific data type that determines the kind of data it can hold. Let's explore how to declare variables and the fundamental data types in C#.
1. Declaring Variables:
In C#, you declare a variable by specifying its data type, followed by the variable name. Here's a basic example:
In this example, an integer variable named age is declared, initialized with the value 25, and then displayed on the console.
2. Data Types:
C# supports various data types. Here are some of the fundamental ones:
Numeric Types:
int: 32-bit signed integer.float: 32-bit single-precision floating-point.double: 64-bit double-precision floating-point.decimal: 128-bit high-precision decimal.
Boolean Type:
bool: Represents a Boolean value (trueorfalse).
Character Type:
char: Represents a single Unicode character.
String Type:
string: Represents a sequence of characters.
3. Example Using Different Data Types:
Let's create a program that uses various data types:
4. String Interpolation:
In the example above, string interpolation is used with the $ symbol before the string. This allows you to embed expressions inside string literals, making it more readable.
5. Run the Program:
Press F5 or click the "Start Debugging" button to build and run your program. You should see the output with values for each variable.
6. Experiment:
- Try changing the values of variables and see how it affects the output.
- Declare variables of different data types and experiment with their values.
Congratulations! You've learned about variables and fundamental data types in C#. In the next tutorial, we'll explore control flow statements, including if statements and loops.