C# Data Types
C# Types CIL Types Size/Capacity Default Value
Integer Types
byte [Link] 1 byte (0 - 255) 0
short System.Int16 2 bytes (-2 ^ 15 to 2 ^ 15 - 0
1)
int System.Int32 4 bytes (-2 ^ 31 to 2 ^ 31 - 0
1)
long System.Int64 8 bytes (-2 ^ 63 to 2 ^ 63 - 0
1)
sbyte [Link] 1 byte (-128 to 127) 0
ushort System.UInt16 2 bytes (0 to 2 ^ 16 - 1) 0
uint System.UInt32 4 bytes (0 to 2 ^ 32 - 1) 0
ulong System.UInt64 8 bytes (0 to 2 ^ 64 - 1) 0
Decimal Types
float [Link] 4 bytes 0
double [Link] 8 bytes 0
decimal [Link] 16 bytes 0
Boolean Type
bool [Link] 1 byte False
DateTime Type
DateTime [Link] 8 bytes 01/01/0001 [Link]
Unique Identifier Type
Guid [Link] 32 bytes 00000000-0000-0000-0000-
000000000000
Character Types
char [Link] 2 bytes \0
string [Link] Null
Base Type
object [Link] Null
1. Data Types in C#
Data types specify what kind of data a variable can store and how much memory it
occupies.
C# data types are broadly classified into:
• Primitive (Predefined)
• Abstract (Developer-defined)
2. Categories of Data Types
A. Value Types
• Store actual data directly
• Fast access
• Stored in Stack memory
• Memory allocated at compile time
• Smaller size
• Changes affect only the variable itself
Examples:
int, float, char, bool, struct, enum
B. Reference Types
• Store reference (address) to actual data
• Stored in Heap memory
• Memory allocated at runtime
• Used for large and complex data
• Changes affect all references pointing to the same object
Examples:
class, interface, array, delegate, string, object
3. Value Types – Simple Types
A. Numeric Types
1. Integral (Non-Floating Point)
Type Size Range
byte 1 byte 0 to 255
sbyte 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
ushort 2 bytes 0 to 65,535
int 4 bytes -2,147,483,648 to
2,147,483,647
uint 4 bytes 0 to 4,294,967,295
long 8 bytes Very large range
ulong 8 bytes Only positive values
2. Floating-Point Types
Type Size Use
float 4 bytes Single precision
double 8 bytes Double precision
decimal 16 bytes High precision (financial
calculations)
B. Character Type
• char
• 2 bytes
• Uses Unicode
• Range: \u0000 to \uFFFF
C. Boolean Type
• bool
• 1 byte
• Values: true or false
D. Date and Time
• DateTime
• Stores date and time values
• Range:
01/01/0001 [Link] to 31/12/9999 [Link]
4. Reference Types
A. String
• string is a class
• Stored in Heap
• Immutable (cannot be changed once created)
• Maximum size: 2 GB
• Stores Unicode characters
B. Class
• Blueprint for creating objects
• Can contain fields, methods, constructors
C. Interface
• Contains method declarations only
• Supports multiple inheritance
D. Arrays
• Stores multiple values of same data type
• Fixed size
• Stored in Heap
E. Delegate
• Reference type used to store method references
• Supports event handling and callbacks
Below are simple C# programs related to data types, written in an easy, exam-
friendly way. These examples cover value types, reference types, stack vs heap
behavior, and basic usage.
1. Program Using Simple Value Types
using System;
class Program
{
static void Main()
{
int age = 18;
float percentage = 85.5f;
char grade = 'A';
bool isPassed = true;
[Link]("Age: " + age);
[Link]("Percentage: " + percentage);
[Link]("Grade: " + grade);
[Link]("Passed: " + isPassed);
}
}
2. Program to Show Value Type Behavior
(Value types store data directly)
using System;
class Program
{
static void Main()
{
int a = 10;
int b = a; // copy of value
b = 20;
[Link]("a = " + a);
[Link]("b = " + b);
}
}
Output:
a = 10
b = 20
✔ Changing b does not affect a.
3. Program Using Reference Type (Class)
using System;
class Student
{
public int marks;
}
class Program
{
static void Main()
{
Student s1 = new Student();
[Link] = 80;
Student s2 = s1; // reference copy
[Link] = 95;
[Link]("s1 marks: " + [Link]);
[Link]("s2 marks: " + [Link]);
}
}
Output:
s1 marks: 95
s2 marks: 95
✔ Both refer to the same memory location (Heap).
4. Program Using Enum
using System;
enum Days
{
Monday, Tuesday, Wednesday, Thursday, Friday
}
class Program
{
static void Main()
{
Days today = [Link];
[Link]("Today is: " + today);
}
}
5. Program Using Struct
using System;
struct Employee
{
public int id;
public string name;
}
class Program
{
static void Main()
{
Employee emp;
[Link] = 101;
[Link] = "Rahul";
[Link]([Link]);
[Link]([Link]);
}
}
✔ struct is a value type.
6. Program Using String (Reference Type)
using System;
class Program
{
static void Main()
{
string name = "CSharp";
name = name + " Language";
[Link](name);
}
}
✔ String is immutable (creates new object).
7. Program Using Array
using System;
class Program
{
static void Main()
{
int[] numbers = { 10, 20, 30, 40 };
for (int i = 0; i < [Link]; i++)
{
[Link](numbers[i]);
}
}
}
8. Program Using DateTime
using System;
class Program
{
static void Main()
{
DateTime today = [Link];
[Link]("Current Date and Time: " + today);
}
}
9. Program Showing Size of Data Types
using System;
class Program
{
static void Main()
{
[Link](sizeof(int));
[Link](sizeof(char));
[Link](sizeof(float));
[Link](sizeof(double));
}
}
1. Variables
• Variables do not have default values in C#.
• They must be initialized either:
o At the time of declaration, or
o Before they are used (consumption).
Example:
int x = 100;
2. Access Modifiers
• Modifiers define the scope and accessibility of a variable or class.
• The default scope of every member in a class or language is private.
• Access modifiers include:
o private
o public
o protected
o internal
Example:
public int number;
private string name;
3. const Keyword
• const is used to declare constant variables.
• The value of a const variable:
o Must be assigned at the time of declaration
o Cannot be changed after declaration
Example:
const float pi = 3.14f; // Declaration and Initialization
4. readonly Keyword
• readonly is used to declare read-only fields.
• The value:
o Can be assigned at declaration or inside a constructor
o Cannot be modified after initialization
Example:
readonly float pi; // Declaration
pi = 3.14f; // Initialization
5. Decimal and Floating-Point Values
• By default, decimal values are treated as double by the compiler.
• To specify other numeric types, use suffixes:
o f → float
o d → double
o m → decimal
Example:
float pi1 = 3.14f;
double pi2 = 3.14d;
decimal pi3 = 3.14m;
Important Note
• Always initialize variables before use.
• Use const when the value should never change.
• Use readonly when the value is set once after object creation.
Nullable Value Types:
These are introduced in C# 2.0 for storing null values under value types because, by
default value types can’t store null values under them whereas reference types can
store null values under them.
string str = null; //Valid
object obj = null; //Valid
int i = null; //Invalid
decimal d = null; //Invalid
To overcome the above problem nullable value types came into picture and if we
want a value type as nullable we need to suffix the type with “?” and declare it as
following:
int? i = null; //Valid
decimal? d = null; //Valid
Implicitly typed variables:
This is a new feature introduced in C# 3.0, which allows declaring variables by using
“var” keyword, so that the type of that variable is identified based on the value that is
assigned to it, for example:
var i = 100; //i is of type int
var f = 3.14f; //f is of type float
var b = true; //b is of type bool
var s = "Hello"; //s is of type string
Note: While using implicitly typed variables we have 2 restrictions:
1. We can't declare these variables with-out initialization. E.g.: var x; //Invalid
2. We can use "var" only on variables but not on fields.
Dynamic Type:
This is a new type introduced in C# 4.0, which is very similar to implicitly typed variables
we discussed above, but here in place of "var" keyword we use dynamic.
Differences between "var" and "dynamic"
Var Dynamic
Type identification is performed at compilation Type identification is performed at runtime.
time.
Once the type is identified can’t be changed to a We can change the type of dynamic with a new
new type again. value in every statement.
var v = 100; //v is of type dynamic d = 100; //d is of type int
int v = 34.56;//Invalid d = 34.56;//d is of type double (Valid)
Can’t be declared with-out initialization. Declaration time initialization is only optional.
var v;//Invalid dynamic d;//Valid
d = 100;//d is of type int d
= false; //d is of type
bool
d = "Hello";//d is of type
string d = 34.56; //d is of type
double
Can be used for declaring variables only. Can be used for declaring variables and fields also.