About Lesson
Data types and variables
In C#, data types are used to define the type and size of data that a variable can hold. Variables, on the other hand, are used to store and manipulate data in a program. Here are some commonly used data types and how to declare variables in C#:
- Numeric Data Types:
- Int: Used to store whole numbers. Example: int age = 25;
- Float: Used to store single-precision floating-point numbers. Example: float price = 9.99f;
- Double: Used to store double-precision floating-point numbers. Example: double pi = 3.14159;
- Decimal: Used to store decimal numbers with high precision. Example: decimal salary = 50000.50m;
- Boolean Data Type:
- Bool: Used to store true/false values. Example: bool isComplete = true;
- Character Data Type:
- Char: Used to store a single character. Example: char grade = ‘A’;
- String Data Type:
- String: Used to store a sequence of characters. Example: string name = “John Smith”;
- Date and Time Data Types:
- DateTime: Used to store date and time values. Example: DateTime currentDate = DateTime.Now;
- Arrays:
Arrays allow you to store multiple values of the same data type in a single variable. Example: int[] numbers = { 1, 2, 3, 4, 5 };
- Variables Declaration:
- To declare a variable, you specify the data type followed by the variable name. Example: int age;
- You can also assign an initial value to the variable during declaration. Example: int age = 25;
- Variable Assignment and Modification:
- Once a variable is declared, you can assign a value to it using the assignment operator (=). Example: age = 30;
- You can modify the value of a variable by assigning a new value to it. Example: age = age + 1;
- Constants:
- Constants are variables whose value cannot be changed once assigned. Example: const double pi = 3.14159;
- variable Naming :Variable names must start with a letter or underscore and can contain letters, numbers, and underscores. Example: int student Count;