Variables and Data Types
In C#, variables are used to store and manipulate data. Before using a variable, you need to declare it with a specific data type. C# provides several built-in data types to accommodate different kinds of values.
Integer Variables
Integer variables are used to store whole numbers without any decimal places. In C#, you can declare an integer variable using the int
keyword. Here's an example:
1int age = 30;
In this example, the variable age
is declared as an integer and assigned a value of 30.
Floating-Point Variables
Floating-point variables are used to store numbers with decimal places. In C#, you can declare a floating-point variable using the double
keyword. Here's an example:
1double height = 1.75;
In this example, the variable height
is declared as a double and assigned a value of 1.75.
String Variables
String variables are used to store sequences of characters, such as names, addresses, or messages. In C#, you can declare a string variable using the string
keyword. Here's an example:
1string name = "John";
In this example, the variable name
is declared as a string and assigned the value "John".
Boolean Variables
Boolean variables are used to store either true
or false
values. In C#, you can declare a boolean variable using the bool
keyword. Here's an example:
1bool isStudent = true;
In this example, the variable isStudent
is declared as a boolean and assigned the value true
.
Character Variables
Character variables are used to store single characters. In C#, you can declare a character variable using the char
keyword. Characters are enclosed in single quotes. Here's an example:
1char gender = 'M';
In this example, the variable gender
is declared as a character and assigned the value 'M'.
By declaring variables with the appropriate data types, you can ensure that the values you store and manipulate are compatible with the operations you want to perform.
Now, let's write some code to see these variable declarations in action:
1using System;
2
3public class Program
4{
5 public static void Main()
6 {
7 // Integer Variables
8 int age = 30;
9 Console.WriteLine("My age is " + age);
10
11 // Floating-Point Variables
12 double height = 1.75;
13 Console.WriteLine("My height is " + height);
14
15 // String Variables
16 string name = "John";
17 Console.WriteLine("My name is " + name);
18
19 // Boolean Variables
20 bool isStudent = true;
21 Console.WriteLine("Am I a student? " + isStudent);
22
23 // Character Variables
24 char gender = 'M';
25 Console.WriteLine("My gender is " + gender);
26 }
27}
xxxxxxxxxx
using System;
public class Program
{
public static void Main()
{
// Integer Variables
int age = 30;
Console.WriteLine("My age is " + age);
// Floating-Point Variables
double height = 1.75;
Console.WriteLine("My height is " + height);
// String Variables
string name = "John";
Console.WriteLine("My name is " + name);
// Boolean Variables
bool isStudent = true;
Console.WriteLine("Am I a student? " + isStudent);
// Character Variables
char gender = 'M';
Console.WriteLine("My gender is " + gender);
}
}