Skip to content

Commit fb1ba7c

Browse files
committed
Added OOP examples
1 parent 0111485 commit fb1ba7c

7 files changed

+256
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
obj
2+
bin
3+
*.sln

classes.cs

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
2+
using System;
3+
4+
namespace ClassDemo
5+
{
6+
// This class demonstrates various ways to declare a class,
7+
// different property and field implementations, and object creation methods.
8+
public class Person
9+
{
10+
// Private fields
11+
// usually their names start with _ (underscore)
12+
private string _firstName;
13+
private string _lastName;
14+
private int _age;
15+
16+
// Auto-implemented property
17+
public string Email { get; set; }
18+
19+
// Property with a backing field
20+
private string _phoneNumber;
21+
public string PhoneNumber
22+
{
23+
get { return _phoneNumber; }
24+
set { _phoneNumber = value; }
25+
}
26+
27+
// Read-only property
28+
public string FullName
29+
{
30+
get { return $"{_firstName} {_lastName}"; }
31+
}
32+
33+
// Property with private setter
34+
public int Age
35+
{
36+
get { return _age; }
37+
private set { _age = value; }
38+
}
39+
40+
// Static members ( a static property)
41+
public static int Population { get; private set; }
42+
43+
// Default constructor
44+
public Person()
45+
{
46+
_firstName = "Unknown";
47+
_lastName = "Unknown";
48+
Age = 0;
49+
Population++;
50+
}
51+
52+
// Parameterized constructor
53+
public Person(string firstName, string lastName, int age)
54+
{
55+
_firstName = firstName;
56+
_lastName = lastName;
57+
Age = age;
58+
Population++;
59+
}
60+
61+
// Method to demonstrate changing private field through public method
62+
public void UpdateName(string firstName, string lastName)
63+
{
64+
_firstName = firstName;
65+
_lastName = lastName;
66+
}
67+
68+
// Method to print Person's detail
69+
public string DisplayInfo()
70+
{
71+
return $"{FullName}, Age: {Age}, Email: {Email}, Phone: {PhoneNumber}";
72+
}
73+
74+
// Static method
75+
public static void ShowPopulation()
76+
{
77+
Console.WriteLine($"Current population: {Population} people created.");
78+
}
79+
}
80+
81+
class Program
82+
{
83+
static void Main()
84+
{
85+
// Object creation Using default constructor
86+
Person person1 = new();
87+
Console.WriteLine("Person 1:\n" + person1.DisplayInfo());
88+
89+
// Object creation Using parameterized constructor
90+
Person person2 = new("Faqeer", "Hussain", 41);
91+
person2.Email = "[email protected]";
92+
person2.PhoneNumber = "03320684191";
93+
Console.WriteLine("Person 2:\n" + person2.DisplayInfo());
94+
95+
// Object creation Using object initializer syntax
96+
Person person3 = new()
97+
{
98+
Email = "[email protected]",
99+
PhoneNumber = "0300000000000"
100+
};
101+
person3.UpdateName("Alice", "Brown");
102+
Console.WriteLine("Person 3:\n" + person3.DisplayInfo());
103+
104+
// Accessing static method
105+
Person.ShowPopulation();
106+
}
107+
}
108+
}

classes.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<Compile Include="classes.cs" />
9+
</ItemGroup>
10+
</Project>

events.cs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
using System;
3+
4+
namespace EventDemo
5+
{
6+
// Step 1: Define a delegate
7+
public delegate void SomeEventHandler(object sender, EventArgs e);
8+
9+
// Publisher class
10+
class Publisher
11+
{
12+
// Step 2: Declare the event
13+
public event SomeEventHandler SomeEvent;
14+
15+
// Method to raise the event
16+
public void NotifyAboutEvent(EventArgs e)
17+
{
18+
// Step 3: Raise/Invoke the event
19+
if (SomeEvent != null) // Check if there are any subscribers
20+
{
21+
SomeEvent(this, e); // Raise/Invoke the event
22+
}
23+
}
24+
25+
// Method to simulate an action that triggers the event
26+
public void DoSomething()
27+
{
28+
Console.WriteLine("Publisher is doing something...");
29+
// Raise the event when the action occurs
30+
NotifyAboutEvent(EventArgs.Empty);
31+
}
32+
}
33+
34+
// Subscriber class
35+
class Subscriber
36+
{
37+
// Event handler method
38+
public void HandleSomeEvent(object sender, EventArgs e)
39+
{
40+
Console.WriteLine("Event received by Subscriber.");
41+
}
42+
}
43+
44+
class Program
45+
{
46+
static void Main(string[] args)
47+
{
48+
Publisher publisherObject = new();
49+
Subscriber subscriberObject = new();
50+
51+
// subscriberObject subscribes to SomeEvent
52+
// which is declared in publisherObject's class
53+
// by using '+=' operator
54+
publisherObject.SomeEvent += subscriberObject.HandleSomeEvent;
55+
56+
// Trigger the action to raise event in the publisher
57+
publisherObject.DoSomething();
58+
}
59+
}
60+
}

events.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<Compile Include="events.cs" />
9+
</ItemGroup>
10+
</Project>

inheritance.cs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
using System;
3+
4+
namespace InheritanceDemo
5+
{
6+
// this is our Base/Parent class
7+
public class Person
8+
{
9+
// Base/Parent class properties
10+
public string Name { get; set; }
11+
public int Age { get; set; }
12+
13+
// Base/Parent class method
14+
public void DisplayPersonInfo()
15+
{
16+
Console.WriteLine($"Name: {Name}, Age: {Age}");
17+
}
18+
}
19+
20+
// Implementing Inheritance for Student Class
21+
public class Student : Person
22+
{
23+
// Derived/Child class Property
24+
public string RollNumber { get; set; }
25+
26+
// Derived/Child class method
27+
public void DisplayStudentInfo()
28+
{
29+
Console.WriteLine($"Student Roll Number: {RollNumber}");
30+
}
31+
}
32+
33+
class Program
34+
{
35+
static void Main()
36+
{
37+
// Creating an object of the derived/child class
38+
// through Object-initializer technique
39+
// accessing properties declared in both
40+
// Parent and Child classes
41+
Student Asfar = new()
42+
{
43+
Name = "Asfar Hussain",
44+
Age = 12,
45+
RollNumber = "IT-2013"
46+
};
47+
48+
// Accessing base/parent class method
49+
Asfar.DisplayPersonInfo();
50+
51+
// Accessing derived/child class method
52+
Asfar.DisplayStudentInfo();
53+
}
54+
}
55+
}

inheritance.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<Compile Include="inheritance.cs" />
9+
</ItemGroup>
10+
</Project>

0 commit comments

Comments
 (0)