ุชุนุงูู ูุชุฎูู ุฅููุง ุนุงูุฒูู ูุฎุฒู ุนูุฏูุง ุฃูุงู ุงูุฃุณุจูุน ูู ุงูููุฏ. ููู ุญุฏ ู ู ูู ูููู: โูุฎุฒู ุฃุฑูุงู ุ ุฑูู 1 ุฏุง ุงูุณุจุชุ ูููุฐุงโ. ุจุณ ูู ุงูุทุฑููุฉ ุฏู ู ูุฑูุกุฉ ูู ูููู ุฉ ุจุงููุณุจุงูู ูู Developerุ ุฃููุฏ ูุฃ.
ููุง ุจููุฌู ุฏูุฑ ุงูู Enumุ ููู ุนุจุงุฑุฉ ุนู Data Type ุงูุช ุงููู ุจุชุญุฏุฏ ุงูู Values ุงููู ู ู ูู ูุงุฎุฏูุง. ููุฏุฑ ูุนุชุจุฑู ููุน ุฎุงุต ู ู ุงูู Value Types ุจูุณุชุฎุฏู ุนุดุงู ูุนุฑู ู ุฌู ูุนุฉ ู ู ุงูุซูุงุจุช ุงููู ูููุง ุฃุณู ุงุก (Named Constants).
ุงุณุชุฎุฏุงู ุงูู Enum ุจูุญุณู ุงูู Readability (ูุฑุงุกุฉ ุงูููุฏ) ุจุดูู ูุจูุฑ ุฌุฏุงูุ ูุจูู ูุน ุงุณุชุฎุฏุงู ุงูู โMagic Stringsโุ ููู ุงู ุจูููู ุงุญุชู ุงููุฉ ุฅูู ุฅูุช ูุฒู ุงููู ุงููู ุดุบุงููู ุนูู ููุณ ุงูุจุฑูุฌูุช ุชุบูุทูุง.
Enum Memory & Concept
ุงูู Enum ุจุจุณุงุทุฉ ูู ุฅุนุงุฏุฉ ุชุณู
ูุฉ ูุจุนุถ ุงูููู
ุงูุฑูู
ูุฉ (ุฒู ุงูู int) ูุจุชุฏููุง ุฃุณุงู
ู ุฃู Labels. ูุนูู ูู ูู ุงูุฃุณุงุณ ู
ุญุฌูุฒ ูู ุงูู Memory ูู ู
ุณุงุญุฉ 4 bytes. ุจูุณุชุฎุฏู
ู ูู
ุง ูุจูู ุนูุฏู ู
ุฌู
ูุนุฉ ู
ุญุฏุฏุฉ ู
ู ุงูู Values ูุนุงูุฒ ุฃุนู
ููู
Data Type ุฎุงุตุฉ ุจููู
.
ูุนูู ูู ุฃูุง ุจุฎุฒู ุฃุฑูุงู
ุ ู
ูุทูู ุฃุญุทูุง ูู intุ ุจุณ ูู ุจุฎุฒู ููุน ุงูุฌูุณ (Gender) ุงููู ูู M ุฃู Fุ ูููุง ุงูุฃูุถู ุฃุนู
ููุง Data Type ุฎุงุตุฉ ุจููุง ุจุงุณุชุฎุฏุงู
ุงูู Enum.
Basic Syntax & Examples
ุนุดุงู ูุนุฑู ุงูู Enum ุจูุณุชุฎุฏู
ุงูููู
ุฉ ุงูู
ุญุฌูุฒุฉ enum ูุจููุชุจ ุฌูุงู Labels (ู
ู
ูู ุชููู ููู
ุงุช ุฃู ุฃู ุญุงุฌุฉ ุจุชุนุจุฑ ุนู ุงูููู
ุฉ).
Days of the Week Example
// Enum definition
enum WeekDays
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
// Main
WeekDays today = WeekDays.Monday;
Console.WriteLine(today); // Output: MondayGrades and Gender Examples
enum Grades
{
A, B, C, D, E // Labels: may be word or smth
}
enum Gender
{
Male = 1, Female = 2, M = 1, F = 2
}
// Main
Grades myGrade = Grades.A;
myGrade = Grades.C;Enum Values & CLR
ุฒู ู ุง ููููุง ุฅู ุงูู Enum ุจูุชุนุงู ู ุจุงูู Labelsุ ุจุณ ุงูู CLR ูู ุงูุญูููุฉ ู ุด ุจูุชุนุงู ู ุจุงูู Labels ุฏูุ ุงูู CLR ุจูุฏู ููู Label ู ููู ุฑูู ุจูุชุนุงู ู ุจูู.
ุงููุถุน ุงูุงูุชุฑุงุถู (By default) ุฅู ุงูู Values ุจุชุงุนุฉ ุงูู Enum ุจุชููู ู
ู ููุน int. ุฃูู ููู
ุฉ ุจุชุจุฏุฃ ุจู 0 ููู ููู
ุฉ ุจุนุฏูุง ุจุชุฒูุฏ ุจู
ูุฏุงุฑ 1. ุจุณ ุทุจุนุงู ุชูุฏุฑ ุชุบูุฑ ุงูู Default Values ุฏู ูุฃุฑูุงู
ู
ุฎุตุตุฉ ุฒู ู
ุง ุงูุช ุนุงูุฒุ ูู
ู
ูู ูุณุชุฎุฏู
ุงูุฃุฑูุงู
ุฏู ูู IDs.
Custom Values Example
enum Branches
{
SV = 105, NasrCity = 201, Alex = 221, Assuit
// Assuit = 222
// We can use it with IDs
}
enum OrderStatus
{
Pending,
Processing,
Shipped,
Delivered
}
// Main
Branches MYB = new Branches(); // Default int: 0
MYB = Branches.Alex;
OrderStatus status = OrderStatus.Shipped;
if (status == OrderStatus.Shipped)
Console.WriteLine("Your order is on the way!");Size for Variables
ุฒู ู
ุง ููููุงุ ุงูู Enum ุจูุงุฎุฏ 4 bytes ูุฃูู ุจูุนุชุจุฑ int. ุจุณ ูู ุฃู
ุซูุฉ ู
ุนููุฉ (ูู ุฃูุง ุจุฎุฒู 0 ู 1 ู 2 ุจุณ)ุ ุฃูุง ู
ุด ู
ุญุชุงุฌ ูู ุงูู
ุณุงุญุฉ ุฏู ูู ุงูู Memory. ููุง ู
ู
ูู ุฃูููู ุจุจุณุงุทุฉ: โุฎูู ุงูููุน ุงููู ุจูุชุฎุฒู byteโ.
enum Branches : byte // Inheritance but I don't mean here inher
{
// Labels
}Type Casting & Conversions
ุนุดุงู ูุญูู ุจูู ุงูู Enum ูุงูุฃููุงุน ุงูุชุงููุฉุ ุจูุญุชุงุฌ ูุณุชุฎุฏู ุงูู Casting.
enum Colors { Red = 1, Green = 2, Blue = 3, Yellow = 5 }
// Main
Colors c = Colors.Green;
// Enum to int
int value = (int)c; // Output: 2
// Int to enum
Colors color = (Colors)2; // Output: Green
// Enum to string
string name = c.ToString(); // Output: "Green"ููุทุฉ ู
ูู
ุฉ ููุง: ูู ุญุงููุช ุชุฏู ุงูู Enum ุฑูู
int ู
ุจุงุดุฑุฉ ููุฏููู Errorุ ูุงุฒู
ุชุนู
ู Explicit Casting. ููู
ุงู ูู ุฅุฏูุชู ููู
ุฉ (ุฑูู
) ู
ุด ู
ูุฌูุฏุฉ ุฃุตูุงู ูู ุงูู Labelsุ ุจูุทุจุน ุงูุฑูู
ุฒู ู
ุง ูู ู
ู ุบูุฑ ู
ุง ูุถุฑุจ Error.
// int assignment
myGrade = 2; // Error
myGrade = (Grades)2; // Explicit casting: myGrade = Grades.C
// Casting a non-existent value
myGrade = (Grades)8;
Console.WriteLine(myGrade); // Output: 8Control Flow & Iteration
ู
ู
ูู ูุณุชุฎุฏู
ุงูู Enum ู
ุน ุงูู switch ูุงูู foreach ุจุณูููุฉ ุฌุฏุงู ูู ุงูู Real Life Examples.
Switch Case Example
enum UserRole
{
Admin,
Moderator,
Guest
}
// Main
UserRole role = UserRole.Admin;
switch (role)
{
case UserRole.Admin:
Console.WriteLine("Welcome, Admin!");
break;
case UserRole.Moderator:
Console.WriteLine("Moderator panel unlocked.");
break;
case UserRole.Guest:
Console.WriteLine("Limited access.");
break;
}Iterate over Enum Values
foreach (Colors color in Enum.GetValues(typeof(Colors)))
{
Console.WriteLine($"Name: {color}, Value: {(int)color}");
}Bit Flags with Enum
ูู ุนุงูุฒ ู ุซูุงู ุฃุฎุฒู ุฎุตุงุฆุต ูุฎุท ู ุนูู (Font)ุ ูู ูู Bold ููุง ูุฃุ Italicุ ู Underline. ุงูุญู ุงูู ู ูู ููุง ุฅูู ุฃุนู ู Array of Booleans ูุฃุฎุฒู ููู ุงูููู ุฏูุ ููู Index ุจูู ุซู ุญุงุฌุฉ ู ุนููุฉ.
bool[] Flages = new bool[4];ุนููุจ ุงูุทุฑููุฉ ุฏู:
- ูู ุนุงูุฒ ุฃุบูุฑ ูู ุงูููู
ุงููู ุฌูุงู (ูุนูู ุฃุฎูู ูู ุงูุฎุตุงุฆุต ุชุชุบูุฑ)ุ ูุญุชุงุฌ ุฃุณุชุฎุฏู
for loopูุจูุฏุง ุงูู Time ููุจูุงO(N). - ูู
ุงู ุฃูุง ู
ุญุชูุธ ูู ุงูู Memory ููู Array ูููุง ุจู
4 bytes.
ูุจุฏู ู
ุง ุจุนุฑู 4 bytes ุนุดุงู ุฃุนู
ู ุงูุญูุงุฑ ุฏุงุ ู
ู
ูู ุฃุณุชุฎุฏู
1 byte ุฅูู ุฃุนู
ู ูู ุฏุง ุนู ุทุฑูู ุงุณุชุฎุฏุงู
ุงูู Bits. ุฏุง ูู
ุงู ููุฏููู ู
ูุฒุฉ ุฅูู ุฃูุฏุฑ ุฃุบูุฑ ููู ูู Time O(1) ุนู ุทุฑูู ุงุณุชุฎุฏุงู
ุงูู Bitwise Operations ู
ู ุบูุฑ Loop ููุง ุญุงุฌุฉ.
graph LR A[Array Setup: 4 Bytes, Time O N] --> B[Index 0: Bold] A --> C[Index 1: Italic] A --> D[Index 2: Underline] E[Bit Flags Setup: 1 Byte, Time O 1] --> F[Bit 0: 1] E --> G[Bit 1: 0] E --> H[Bit 2: 1] style A fill:#f9f,stroke:#333,stroke-width:2px style E fill:#bbf,stroke:#333,stroke-width:2px
ุนุดุงู ูุนู
ู ูุฏุงุ ุจูุณุชุฎุฏู
Attribute ุงุณู
ู [Flags] (ุฒู ุงูู Decorator ูู Angular ูุฏุงุ ู
ุฌุฑุฏ ุจูุนุฑู ุงูู Compiler ุฏุง ุนุจุงุฑุฉ ุนู ุฅูู). ููู
ุง ุจูุณุชุฎุฏู
ูุ ูู ุงูู Compiler ู
ููุงุด Label ุจูู
ุซู ุงูุฑูู
ุงูู
ุนููุ ุจูุญุงูู ูุฌูุจ ู
ุฌู
ูุนุฉ ุงูู Labels ุงููู ุจุชุฌู
ุนูู
ูููู
ุณูุง.
Permissions Example


[Flags]
// Attribute: Like decorator in Angular
// (Just tell the compiler what is this?)
enum Permisions : byte
{
Read = 8, Write = 0x04, Excute = 0b_0000_0010, Delete = 1, RootUser = 15
// We can set it with any Form, it doesn't matter
}
// Main
Permisions PRM = new Permisions();
Console.WriteLine(PRM); // Output: 0
PRM = Permisions.Read;
Console.WriteLine(PRM); // Output: Read
PRM ^= Permisions.Write; // XOR: Toggle value
Console.WriteLine(PRM); // Output with flags: Write, Read; without: 12
PRM |= Permisions.Excute; // OR: Add new flag
PRM &= ~(Permisions.Excute); // NAND: Remove flag
PRM = (Permisions)15;
Console.WriteLine(PRM);
// Without RootUser label: Delete, Excute, Write, Read
// With RootUser label: RootUser
// Check if We can write
// AND: To check
if ((PRM & Permisions.Write) == Permisions.Write)
Console.WriteLine("True"); // Output: TrueEnum Parsing
ุจุฌุงูุจ ุงููู ุงุชููู ูุง ุนูู ูู ุงูู Type Castingุ ููุฏุฑ ูุนู ู Parse ููู Enum ู ู String. ู ุซูุง ูู ุฌููุง ูุนู ู Enum ูููุตูู ุงูุฃุฑุจุนุฉุ ููุงุฎุฏ ุงุณู ุงููุตู ู ู ุงูู Userุ ููุทุจุนูู ุงูุดููุฑ ุงูู ุฎุตุตุฉ ููู.
using System;
enum Season
{
Spring,
Summer,
Autumn,
Winter
}
class Program
{
static void Main()
{
Console.WriteLine("Enter a season (Spring, Summer, Autumn, Winter):");
string userInput = Console.ReadLine();
try
{
// Parse the user input to a Season enum value
Season season = (Season)Enum.Parse(typeof(Season), userInput, true);
// Display the corresponding month range
switch (season)
{
case Season.Spring:
Console.WriteLine("Spring: March to May");
break;
case Season.Summer:
Console.WriteLine("Summer: June to August");
break;
case Season.Autumn:
Console.WriteLine("Autumn: September to November");
break;
case Season.Winter:
Console.WriteLine("Winter: December to February");
break;
default:
Console.WriteLine("Invalid season.");
break;
}
}
catch (Exception)
{
Console.WriteLine("Invalid input. Please enter a valid season name.");
}
}
}Notes
- ุงูุฌู
ูุฉ ุงูู
ูุชูุจุฉ
enum Branches : byteู ุน ุงูุชุนูููInheritance but I don't mean here inherุ ุงูุตุฏ ุจููุง ุฅู ุฏู ู ุด ุนู ููุฉ โูุฑุงุซุฉโ (Inheritance) ุจุงูู ูููู ุงูู ุชุนุงุฑู ุนููู ูู ุงูู OOP. ุฏู ุจุจุณุงุทุฉ ุทุฑููุฉ ุจูุญุฏุฏ ุจููุง ุงูู Underlying Type (ุงูููุน ุงูุฃุณุงุณู ุงูุฏุงุฎูู) ููู Enum ุนุดุงู ูููุฑ ู ุณุงุญุฉ ูู ุงูู ูู ูุฑูุ ูุจุฏู ู ุง ูุฎุฒู ุงูููู ููintุ ุจูุฎุฒููุง ููbyte.