C# Data Types and Type Differences
1. 🔴 What are the different data types available in C#?
Data types define what kind of value a variable can store, such as numbers, text, characters, or Boolean values.
C# data types are mainly classified into three categories:
-
Value Types
-
Reference Types
-
Pointer Types
1. Value Types
Value types directly contain their data.
Common Value Types:
|
Data Type |
Description |
Example |
|---|---|---|
|
|
Integer numbers |
|
|
|
Large integer numbers |
|
|
|
Single-precision decimal numbers |
|
|
|
Double-precision decimal numbers |
|
|
|
High-precision decimal numbers |
|
|
|
True or false |
|
|
|
Single UTF-16 code unit |
|
|
|
Unsigned integer from 0 to 255 |
|
|
|
Small integer |
|
|
|
User-defined value type |
|
|
|
Named constants |
|
Example:
int age = 25;
double salary = 45000.50;
bool isActive = true;
char grade = 'A';
2. Reference Types
Reference type variables hold references to objects rather than containing the object's data directly.
Common Reference Types:
|
Data Type |
Description |
Example |
|---|---|---|
|
|
Text |
|
|
|
User-defined reference type |
|
|
|
Base type of all C# types |
|
|
|
Collection of elements |
|
|
|
Defines a contract |
|
|
|
References methods |
|
|
|
Reference type with value-based equality |
|
Example:
string name = "John";
int[] numbers = { 10, 20, 30 };
Employee emp = new Employee();
3. Pointer Types
Pointer types store memory addresses and are primarily used in unsafe code.
Example:
unsafe
{
int number = 100;
int* ptr = &number;
Console.WriteLine(*ptr); // 100
}
Pointer code requires unsafe compilation to be enabled.
Data Type Classification
C# Data Types
|
|----------------------|
| | |
v v v
Value Types Reference Pointer
Types Types
| | |
v v v
int string int*
long class char*
float object
double array
decimal interface
bool delegate
char
struct
enum
Key Points
-
Value types directly contain their data.
-
Reference types store references to objects.
-
Pointer types are used in unsafe programming.
-
int,long,float,double, anddecimalare value types. -
string,class, andarrayare reference types. -
All C# types ultimately derive from
System.Object.
Interview Answer: C# data types are mainly classified into value types, reference types, and pointer types. Value types contain their data directly, reference types refer to objects, and pointer types store memory addresses.
2. 🔴 What is the difference between int, long, float, double, and decimal?
These are numeric data types used to store integer and fractional numbers.
The main differences are their size, range, precision, and usage.
Numeric Data Types Comparison
|
Data Type |
Size |
Precision / Range |
Example |
|---|---|---|---|
|
|
4 bytes |
−2.14 billion to +2.14 billion |
|
|
|
8 bytes |
Approximately ±9.22 quintillion |
|
|
|
4 bytes |
~6–9 significant digits |
|
|
|
8 bytes |
~15–17 significant digits |
|
|
|
16 bytes |
28–29 significant digits |
|
1. int – Integer
Used to store whole numbers without decimal values.
int age = 25;
int employeeCount = 100;
Usage: Age, quantity, count, and identifiers within its range.
2. long – Large Integer
Used when the number exceeds the range of int.
long population = 10000000000L;
Usage: Large counters, large integer IDs, and population values.
3. float – Single-Precision Floating Point
Used to store fractional numbers where lower precision is acceptable.
float temperature = 36.5f;
Usage: Graphics, gaming, and memory-sensitive numerical calculations.
4. double – Double-Precision Floating Point
Used to store fractional numbers with higher precision than float.
double distance = 1250.56789;
Usage: Scientific calculations, measurements, and general floating-point calculations.
5. decimal – High-Precision Decimal
Used when decimal precision is important, especially for financial calculations.
decimal salary = 45000.75m;
decimal amount = 1250.50m;
Usage: Banking, salary calculations, prices, and financial transactions.
Why Use decimal Instead of double for Money?
Example:
double a = 0.1;
double b = 0.2;
Console.WriteLine(a + b == 0.3); // False
This happens because double uses binary floating-point representation, which cannot represent many decimal fractions exactly.
Using decimal:
decimal a = 0.1m;
decimal b = 0.2m;
Console.WriteLine(a + b == 0.3m); // True
decimal can represent these particular decimal values exactly, making it more suitable for financial calculations. Rounding rules are still necessary in real financial systems.
Key Points
-
intstores whole numbers up to approximately 2.14 billion. -
longstores larger whole numbers. -
floatprovides lower floating-point precision. -
doubleprovides higher floating-point precision thanfloat. -
decimalis preferred for monetary calculations. -
Use
fforfloat,mfordecimal, and optionallyLforlong.
Interview Answer: int and long store whole numbers, whereas float, double, and decimal store fractional numbers. float has lower precision, double has higher binary floating-point precision, and decimal is preferred for financial calculations.
3. 🔴 What is the difference between string and char?
In C#, char represents a single UTF-16 code unit, whereas string represents a sequence of UTF-16 code units used to store text.
1. char Data Type
The char data type is used to store a single character such as a letter, digit, or symbol that fits in one UTF-16 code unit.
Example:
char grade = 'A';
char digit = '5';
Console.WriteLine(grade); // A
Important: char uses single quotes (' ').
2. string Data Type
The string data type is used to store text containing zero or more characters.
Example:
string name = "John";
string message = "Welcome to C#";
Console.WriteLine(name); // John
Important: string uses double quotes (" ").
Difference Between string and char
|
|
|
|---|---|
|
Value type |
Reference type |
|
Represents one UTF-16 code unit |
Represents a sequence of UTF-16 code units |
|
Uses single quotes |
Uses double quotes |
|
Fixed size of 2 bytes |
Length varies |
|
Example: |
Example: |
|
Mutable variable value |
Immutable string object |
Simple Example
char letter = 'A';
string word = "Apple";
Console.WriteLine(letter); // A
Console.WriteLine(word); // Apple
Can We Convert char to string?
Yes.
char letter = 'A';
string result = letter.ToString();
Console.WriteLine(result); // A
Can We Access a Character from a String?
Yes, using an index.
string name = "John";
char firstLetter = name[0];
Console.WriteLine(firstLetter); // J
Key Points
-
charis a value type, whereasstringis a reference type. -
charuses single quotes;stringuses double quotes. -
charoccupies 2 bytes. -
A
stringcan contain zero or more UTF-16 code units. -
Strings are immutable in C#.
-
A Unicode character outside the Basic Multilingual Plane may require two
charvalues (a surrogate pair).
Interview Answer: char is a value type that represents a single UTF-16 code unit, whereas string is an immutable reference type used to store text.
Quick Interview Revision
|
Concept |
Key Point |
|---|---|
|
Value Types |
Directly contain data |
|
Reference Types |
Hold references to objects |
|
Pointer Types |
Store memory addresses |
|
|
4-byte integer |
|
|
8-byte integer |
|
|
4-byte floating-point number |
|
|
8-byte floating-point number |
|
|
16-byte decimal number, preferred for money |
|
|
Single UTF-16 code unit, value type |
|
|
Immutable text, reference type |
Important Interview Tip: For .NET developer interviews, remember these three distinctions: value vs reference types, double vs decimal, and char vs string. These are frequently tested C# fundamentals.