What are the Data Types in C# ???
Data Types in C# define the type of data that can be stored in a variable. They determine the size and layout of the variable’s memory, the range of values that can be stored, and set of operations that can be performed on the variable. C# data types are divided into two main categories, value types and reference type.
Value types
Value types hold data directly. Examples include:
- Integral types:
int
,byte
,short
,long
,uint
,ulong
,sbyte
- Floating-point types:
float
,double
- Decimal type:
decimal
- Boolean type:
bool
- Character type:
char
- Structures:
struct
- Enumerations:
enum
Example – Data Types in C#
Here’s an example program that demonstrates the use of different Data Types in C#
using System; namespace DataTypesExample { class Program { static void Main(string[] args) { int age = 30; // Integer type double height = 5.9; // Double type string name = "John"; // String type bool isActive = true; // Boolean type char grade = 'A'; // Char type float weight = 70.5f; // Float type decimal salary = 50000.75m; // Decimal type Console.WriteLine("Name: " + name); Console.WriteLine("Age: " + age); Console.WriteLine("Height: " + height); Console.WriteLine("Active: " + isActive); Console.WriteLine("Grade: " + grade); Console.WriteLine("Weight: " + weight); Console.WriteLine("Salary: " + salary); } } }
Line-by-Line Notes
static void Main(string[] args)
- The
Main
method is the entry point of the program. It isstatic
because it is called by the runtime without creating an instance of the class.void
indicates that the method does not return any value.string[] args
is an array of strings that can hold command-line arguments.
int age = 30;
- Declares an integer variable named
age
and initializes it with the value30
. int
: Data type for a 32-bit signed integer.age
: Name of the variable.30
: Initial value assigned toage
.
double height = 5.9;
- Declares a double-precision floating-point variable named
height
and initializes it with the value5.9
. double
: Data type for double-precision floating-point numbers.height
: Name of the variable.5.9
: Initial value assigned toheight
.
string name = "John";
- Declares a string variable named
name
and initializes it with the value"John"
. string
: Data type for a sequence of characters.name
: Name of the variable."John"
: Initial value assigned toname
.
bool isActive = true;
- Declares a boolean variable named
isActive
and initializes it with the valuetrue
. bool
: Data type for boolean values (true
orfalse
).isActive
: Name of the variable.true
: Initial value assigned toisActive
.
char grade = 'A';
- Declares a character variable named
grade
and initializes it with the value'A'
. char
: Data type for a single 16-bit Unicode character.grade
: Name of the variable.'A'
: Initial value assigned tograde
.
float weight = 70.5f;
- Declares a single-precision floating-point variable named
weight
and initializes it with the value70.5
. float
: Data type for single-precision floating-point numbers. Thef
suffix indicates a float literal.weight
: Name of the variable.70.5f
: Initial value assigned toweight
.
decimal salary = 50000.75m;
- Declares a decimal variable named
salary
and initializes it with the value50000.75
. decimal
: Data type for financial and monetary calculations. Them
suffix indicates a decimal literal.salary
: Name of the variable.50000.75m
: Initial value assigned tosalary
.