Unit - II ( Hinglish Version - For Understanding )
1. C# Object-Oriented Programming (OOPs)
C# ek Object-Oriented Programming (OOP) language hai, jo code ko objects ke around organize karta hai, actions ke bajaye. Yahan pe details diye gaye hain:
1.1. Encapsulation
Encapsulation ka matlab hota hai kisi object ki internal state ko chhupana aur sirf wohi cheez expose karna jo dusre parts ko interact karne ke liye zaroori ho. Yeh access modifiers aur properties ke through achieve hota hai. Encapsulation kyu important hai?
Data Protection: Yeh ensure karta hai ki object ka internal state unintended ya unauthorized modification se bach sake.
Code Maintenance: Tum internal logic ya implementation ko bina external interface ko affect kiye modify kar sakte ho, jisse system flexible aur maintainable banta hai.
Example:
Yahan pe balance BankAccount class ke andar encapsulated hai, aur usko Deposit aur GetBalance jaise public methods ke through control kiya jata hai.
1.2. Inheritance
Inheritance tumhe code reuse karne ka option deta hai, jisme ek class doosri class se members (fields aur methods) inherit karti hai. Isse tum existing class ka behavior extend ya modify kar sakte ho, bina redundancy ke. Inheritance types in C#:
Single Inheritance: Ek class sirf ek base class se inherit kar sakti hai.
Multilevel Inheritance: Ek class doosri derived class se inherit karti hai, jo apne aap kisi aur class se inherit karti hai (hierarchy banati hai).
Example:
Car class Vehicle se inherit kar rahi hai, isliye usse StartEngine() method bhi mil jata hai. Yeh inheritance ka faida dikhata hai.
Key Concepts:
Overriding Methods: Derived classes base class ke methods ko override kar sakti hain taaki custom functionality provide ki ja sake. Yeh
virtual
keyword ke saath base class mein auroverride
keyword ke saath derived class mein hota hai.
Example:
1.3. Polymorphism
Polymorphism ka matlab hai ki alag-alag types ke objects ko ek common base type ke object ke roop mein treat kiya ja sakta hai. Isse ek hi method ya property alag-alag objects ke liye alag tarike se behave kar sakti hai. Types of Polymorphism:
Compile-time Polymorphism: Yeh method overloading aur operator overloading ke through achieve hota hai.
Run-time Polymorphism: Yeh method overriding ke through achieve hota hai, jisme base class ke reference ko derived class ke objects ke liye use kiya jata hai.
Example of Compile-time Polymorphism (Method Overloading):
Yahan Add method ko integers aur doubles dono ke liye overload kiya gaya hai, jo same functionality provide karta hai but alag input types ke saath.
Example of Run-time Polymorphism (Method Overriding):
Agar tumhare paas ek Dog object ho lekin use Animal type ke reference se reference kiya ho, to Speak() method dog ke version ko call karega.
1.4. Object Lifetime
C# mein objects ko automatically Garbage Collector (GC) ke dwara create aur destroy kiya jata hai. GC memory manage karta hai aur un objects ko free kar deta hai jo ab referenced nahi hote. Types of Objects:
Reference Types (Classes, Arrays, Strings): Heap pe stored hote hain aur GC ke through manage kiye jaate hain.
Value Types (int, double, struct): Stack pe stored hote hain.
GC automatic run hota hai aur memory reclaim karta hai, lekin tum apni taraf se object lifetime control kar sakte ho using finalizers ya IDisposable interface implement karke.
Example:
1.5. Components and Modules
Components ek self-contained, reusable units of functionality hoti hain. Modules wo hote hain jo related components ka group hote hain. Yeh terms large software systems ko chhote aur manageable pieces mein organize karne mein madad karti hain.
Example:
Component: Ek authentication ka class library jo alag-alag applications mein reuse ho sakta hai.
Module: Ek set of components jo user authentication, logging aur error management handle karta hai.
Use in C#:
Assemblies: Components ko assemblies mein package kiya jata hai jo DLLs ya EXEs ho sakte hain.
Namespaces: C# mein components ko logically organize karne ke liye namespaces use hote hain, jaise
System.Collections.Generic
jo collection classes ko define karta hai.
1.6. Windows Forms
Windows Forms ek GUI framework hai jo C# mein desktop applications build karne ke liye use hota hai. Yeh drag-and-drop approach ko use karta hai user interface design karte waqt, jisse graphical elements like buttons, textboxes aur menus ko easily develop kiya ja sakta hai.
Key Components:
Controls: UI elements jaise buttons, labels, aur textboxes.
Events: Actions jaise button clicks ya key presses jo functions ko trigger karte hain.
Form: Application ka main window.
Example:
1.7. Interface
C# mein interface ek contract define karta hai jo methods (implementation ke bina) ka hota hai, jise class ko implement karna padta hai. Interfaces loose coupling ko promote karte hain, jisme class ko specific base class ki jagah behaviors implement karne ka option milta hai.
Key Points:
Multiple Inheritance: C# mein ek class multiple interfaces ko implement kar sakti hai, jo class ko multiple behaviors inherit karne ka option deta hai.
Interface vs Abstract Class: Interface sirf abstract hota hai, jabki abstract class concrete aur abstract methods dono ko implement kar sakta hai.
Example:
1.8. Cloneable Objects
C# mein objects ko cloneable banaya ja sakta hai agar tum ICloneable interface implement karo. Isse tum Clone() method ka use kar ke object ka shallow ya deep copy bana sakte ho.
Shallow Clone: Sirf object ko copy kiya jata hai, references ko nahi.
Deep Clone: Ek naya instance create kiya jata hai aur saare referenced objects bhi copy kiye jaate hain.
Example of ICloneable:
1.9. Comparable Objects
Agar object ko comparable banana hai to usse IComparable<T>
interface implement karna padta hai. Yeh tumhe define karne ka option deta hai ki tumhare objects kaise compare honge using CompareTo
method.
Example:
1.10. Collections and Namespaces
C# mein collections objects ka group hoti hain, jaise arrays, lists, dictionaries, aur queues. Yeh collections alag-alag namespaces mein organized hoti hain, jaise System.Collections
aur System.Collections.Generic
.
Common Collections:
List: Ek dynamic array jo automatically resize hota hai.
Dictionary<TKey, TValue: Key-value pairs ka collection.
Queue: FIFO (First-In-First-Out) collection.
Stack: LIFO (Last-In-First-Out) collection.
2. Advanced Class Construction
2.1. Custom Indexer
Indexer ek object ko array ki tarah index karne ka option deta hai, jo custom behavior ke liye use kiya ja sakta hai.
2.2. Overloading Operators
C# mein tum operators ko overload kar sakte ho taaki unke liye custom behavior define kiya ja sake.
2.3. Delegates
Delegate ek type hota hai jo specific parameter list aur return type ke methods ko reference karta hai. Yeh callback methods aur event handling ke liye commonly use hota hai.
2.4. Events
Event ek tarika hota hai jisme ek class doosri classes ya objects ko kisi occurrence ke baare mein notify karti hai, jo observer pattern ko follow karta hai. Event delegates pe based hota hai.
Last updated