0% found this document useful (0 votes)
14 views6 pages

unit2

Unit 2 of Net

Uploaded by

ixiixi439
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

unit2

Unit 2 of Net

Uploaded by

ixiixi439
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Unit II: C# Object-Oriented Programming

1. OOP Concepts in C#

 Object-Oriented Programming (OOP):


Programming paradigm based on objects containing data (fields) and methods. Core
principles include:

o Encapsulation: Wrapping data and methods into a single unit (class).

o Inheritance: Deriving new classes from existing ones.

o Polymorphism: Using a single interface to represent different data types.


o Abstraction: Hiding implementation details and showing only the essentials.

2. Encapsulation

 Definition: Restricting access to some of an object's components.


 Implementation: Using access modifiers (private, protected, public, etc.).

 Example:

class BankAccount

private double balance;

public double Balance

get { return balance; }

set { if (value >= 0) balance = value; }

}
}

3. Inheritance

 Definition: A class can inherit members from another class using the : symbol.
 Base Class: Parent class providing members.

 Derived Class: Child class inheriting from the base class.

 Example:

class Animal

{
public void Speak() => Console.WriteLine("Animal sound");
}

class Dog : Animal

public void Bark() => Console.WriteLine("Dog barks");

4. Polymorphism

 Compile-Time Polymorphism: Achieved using method overloading.

 Run-Time Polymorphism: Achieved using method overriding with virtual and override
keywords.
 Example:

class Shape
{

public virtual void Draw() => Console.WriteLine("Drawing a shape");

class Circle : Shape

{
public override void Draw() => Console.WriteLine("Drawing a circle");

5. Object Lifetime

 Definition: The lifecycle of an object from creation to destruction.

 Key Methods:

o Constructor: Initializes the object.

o Destructor: Cleans up resources.

o Garbage Collection: Managed by CLR to free unused objects.

class Example

{
public Example() { Console.WriteLine("Constructor called"); }
~Example() { Console.WriteLine("Destructor called"); }

6. Components and Modules

 Components: Reusable and self-contained pieces of code.

 Modules: Logical units or parts of a program (often organized into assemblies).


 Example: Windows Forms components like Button, TextBox.

7. Windows Forms

 Definition: GUI toolkit for creating desktop applications.


 Key Controls: Button, Label, TextBox, ListBox, etc.

 Example:

private void button1_Click(object sender, EventArgs e)

MessageBox.Show("Button clicked!");

8. Interfaces
 Definition: A contract that defines methods/properties a class must implement.

 Syntax:

interface IAnimal
{

void Speak();
}

class Dog : IAnimal

public void Speak() => Console.WriteLine("Dog barks");

}
9. Cloneable and Comparable Objects
 Cloneable:
Implemented using ICloneable interface for deep/shallow copy.

class Person : ICloneable

{
public string Name { get; set; }

public object Clone() => MemberwiseClone();


}

 Comparable:
Used to compare objects, implemented via IComparable.
class Student : IComparable<Student>

{
public int Marks { get; set; }

public int CompareTo(Student other) => Marks.CompareTo(other.Marks);

10. Collections Namespace

 Common Classes: List<T>, Dictionary<TKey, TValue>, Stack<T>, Queue<T>.


 Example:

List<int> numbers = new List<int> { 1, 2, 3 };

numbers.Add(4);

Console.WriteLine(numbers[2]);

11. Advanced Class Construction

a. Custom Indexers
 Definition: Allows objects to be indexed like arrays.

 Example:
class Sample

private int[] data = new int[10];

public int this[int index]

{
get => data[index];
set => data[index] = value;

b. Operator Overloading

 Definition: Defining behavior for operators with custom types.

 Example:
class Complex

public int Real { get; set; }

public int Imaginary { get; set; }


public static Complex operator +(Complex c1, Complex c2) =>

new Complex { Real = c1.Real + c2.Real, Imaginary = c1.Imaginary + c2.Imaginary };

c. Delegates

 Definition: Type-safe function pointers.

 Syntax:
delegate void MyDelegate(string message);

 Example:
class Program

public static void PrintMessage(string message) => Console.WriteLine(message);


static void Main()

{
MyDelegate del = PrintMessage;

del("Hello, Delegates!");

d. Events

 Definition: A special delegate type for signaling state changes.

 Example:
class Publisher
{

public event Action OnNotify;

public void Notify() => OnNotify?.Invoke();

class Subscriber
{

static void Main()

Publisher pub = new Publisher();


pub.OnNotify += () => Console.WriteLine("Event triggered!");

pub.Notify();

You might also like