Value Data Types
These store the actual value directly in memory. The value is stored in the variable itself, so
each variable has its own copy of the data. Changes made to one variable do not affect
another variable.
Enumeration (enum) in C#
An enumeration (enum) in C# is a special value type used to define a set of named
constants.
It improves:
Code readability
Maintainability
Type safety
Instead of using numbers directly, you use meaningful names.
Syntax
enum EnumName
{
Value1,
Value2,
Value3
}
Example:
enum Days
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
Default Values
By default:
First enum value = 0
Next values increase by 1
Example:
enum Numbers
{
One, // 0
Two, // 1
Three // 2
}
Assigning Custom Values
You can assign your own integer values.
enum Status
{
Pending = 1,
Approved = 2,
Rejected = 3
}
Using Enum in Program
Example:
using System;
enum Days
{
Monday,
Tuesday,
Wednesday
}
class Program
{
static void Main()
{
Days today = [Link];
[Link](today);
}
}
Output:
Tuesday
Enum to Integer Conversion
enum Color
{
Red = 1,
Green = 2,
Blue = 3
}
class Program
{
static void Main()
{
int value = (int)[Link];
[Link](value);
}
}
Output:
2
Integer to Enum Conversion
int num = 1;
Days day = (Days)num;
[Link](day);
Output:
Tuesday
Enum Underlying Types
Default underlying type is int.
You can also use:
byte
short
long
Example:
enum Size : byte
{
Small = 1,
Medium = 2,
Large = 3
}
Advantages of Enum
Makes code easy to understand
Reduces use of magic numbers
Provides compile-time checking
Improves program maintenance
Limitations of Enum
Enum values are fixed constants
Cannot inherit from another enum
Cannot store methods directly
Real-Life Uses of Enum
Enums are commonly used for:
Days of week
Months
Status codes
Directions
User roles
Traffic signals
Important Points
Enum is a value type
Stored in [Link]
Values are constants
Can be converted to/from integers
Short Example
using System;
enum Grade
{
A,
B,
C,
D,
F
}
class Program
{
static void Main()
{
Grade studentGrade = Grade.A;
[Link](studentGrade);
}
}
Output-
A
Structures
In C#, a structure (struct) is a value type. It helps us to make a single variable that hold
related variables of various data types. The struct keyword is used for creating a structure.
Structures are used to represent a record. Suppose you want to keep track of your books in a
library. You might want to track the following attributes about each book −
Title
Author
Subject
Book ID
It is similar to a class, but:
struct is a value type
class is a reference type
Structures are useful for small data objects.
Syntax
struct Student
public int id;
public string name;
Creating and Using a Structure
using System;
struct Student
{
public int id;
public string name;
}
class Program
{
static void Main()
{
Student s1;
[Link] = 101;
[Link] = "Rahul";
[Link]([Link]);
[Link]([Link]);
}
}
Output
101
Rahul
Features of Structures
1. Value Type
Structures store data directly.
struct Point
{
public int x;
public int y;
}
Copying a structure creates a separate copy.
Point p1;
p1.x = 10;
p1.y = 20;
Point p2 = p1;
p2.x = 50;
[Link](p1.x); // 10
[Link](p2.x); // 50
2. Can Have Methods
struct Rectangle
{
public int length;
public int width;
public int Area()
{
return length * width;
}
}
3. Constructors in Structure
struct Employee
{
public int id;
public string name;
public Employee(int i, string n)
{
id = i;
name = n;
}
}
Usage:
Employee e = new Employee(1, "Aman");
Difference Between Structure and Class
Structure Class
Value type Reference type
Stored in stack (generally) Stored in heap
Cannot inherit another struct/class Supports inheritance
Faster for small objects Better for large objects
No destructor Can have destructor
Important Points
Structures are best for:
o Coordinates
o Colors
o Complex numbers
o Small data records
Structures:
o cannot inherit classes
o can implement interfaces
o can contain constructors, methods, fields, and properties
Advantages of Structures
Memory efficient
Faster performance for small objects
Simple data grouping
Disadvantages
No inheritance
Copying large structs can reduce performance
Limited flexibility compared to classes
When to Use Struct
Use struct when:
Object size is small
Data is immutable/simple
No inheritance required
High performance needed
Arrays in C#
An array is a linear data structure that stores a fixed-size sequence of elements of the same
data type in contiguous memory locations. It allows accessing elements using an index,
starting from 0.
An array can contain primitive data types as well as objects of a class depending on the
definition of an array.
Whenever use primitives data types, the actual values have to be stored in contiguous
memory locations.
Features of Arrays
Stores multiple values
Fixed size
Same data type elements
Indexed collection
Index starts from 0
Declaring an Array
You declare an array by specifying the data type followed by square brackets [].
int[] numbers;
Initializing an Array
You can initialize the array in different ways:
1. With size
int[] numbers = new int[5];
2. With values
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
3. Short syntax
int[] numbers = { 1, 2, 3, 4, 5 };
Accessing Array Elements
Array elements are accessed using an index (starting from 0).
[Link](numbers[0]); // Output: 1
Modifying Array Elements
numbers[0] = 10;
Looping Through an Array
Using for loop
for (int i = 0; i < [Link]; i++)
[Link](numbers[i]);
Using foreach loop
foreach (int num in numbers)
[Link](num);
}
Types of Arrays in C#
1. Single-dimensional array
int[] arr = { 1, 2, 3 };
2. Multi-dimensional array
int[,] matrix = {
{1, 2},
{3, 4}
};
3. Jagged array (array of arrays)
A jagged array is an array of arrays, where each element is itself an array that can
have a different length. Rows are fixed at declaration, but columns can vary. It can
also combine with multidimensional arrays.
int[][] jagged = new int[2][];
jagged[0] = new int[] { 1, 2 };
jagged[1] = new int[] { 3, 4, 5 };
Example Program
using System;
class Program
{
static void Main()
{
int[] numbers = { 10, 20, 30, 40 };
foreach (int num in numbers)
{
[Link](num);
}
}
}