C# Variable & Data Types
Objectives
- What is Variable and Data Types?
- Integers (int, long, short)
- Floating-point numbers (float, double)
- Characters (char)
- Booleans (bool)
- Strings (string)
- Enums
What is Variable and Data Types?
Variable is a named storage location in the computer's memory where you can store data for later use in your program.
Data type specifies the type of data that a variable can hold.
Integers
It represents whole numbers without any fractional:
- int: Represents signed 32-bit integers.
- long: Represents signed 64-bit integers.
- short: Represents signed 16-bit integers.
int age = 30;
long population = 7800000000;
short temperature = -10;
Floating-point numbers (float, double)
It represent numbers with a fractional:
- float: Represents single-precision floating-point numbers.
- long: Represents signed 64-bit integers.
- double: Represents double-precision floating-point numbers.
float pi = 3.14f;
double gravity = 9.81;
Characters (char)
Used to represent single characters:
- char: Represents a single Unicode character.
char grade = 'A';
char symbol = '$';
Booleans (bool)
Used to represent true or false values:
- bool: Represents Boolean values (true or false).
bool isRaining = true;
bool hasPassedExam = false;
Strings (string)
Used to represent sequences of characters:
- string: Represents a sequence of Unicode characters.
string name = "John Doe";
string message = "Hello, World!";
Enums
Used to define a set of named integral constants:
- enum: Defines a new enumeration type with a set of named constants.
enum DaysOfWeek
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
DaysOfWeek today = DaysOfWeek.Wednesday;