Skip to content

Commit 1b873f9

Browse files
committed
Practice on Observable and Observer completed
1 parent a23eb16 commit 1b873f9

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>_21_IObservable_IObserver</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
</Project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Console.WriteLine();
2+
3+
#region Practice 1
4+
5+
MyObservable observable = new();
6+
7+
using var sub1 = observable.Subscribe(new MyObserver("1"));
8+
using var sub2 = observable.Subscribe(new MyObserver("2"));
9+
using var sub3 = observable.Subscribe(new MyObserver("3"));
10+
11+
observable.NotifyObservers(10);
12+
observable.NotifyObservers(20);
13+
observable.NotifyObservers(30);
14+
15+
16+
17+
class MyObservable : IObservable<int>
18+
{
19+
List<IObserver<int>> observers = new();
20+
public IDisposable Subscribe(IObserver<int> observer)
21+
{
22+
if (!observers.Contains(observer))
23+
observers.Add(observer);
24+
25+
return new Unsubscription(() =>
26+
{
27+
observers.Remove(observer);
28+
observer.OnCompleted();
29+
});
30+
}
31+
32+
public void NotifyObservers(int value) => observers.ForEach(observer => observer.OnNext(value));
33+
}
34+
35+
class Unsubscription(Action unSubscription) : IDisposable
36+
{
37+
public void Dispose()
38+
{
39+
unSubscription.Invoke();
40+
unSubscription = null;
41+
}
42+
}
43+
44+
class MyObserver(string observerName) : IObserver<int>
45+
{
46+
public void OnCompleted()
47+
{
48+
Console.WriteLine($"{observerName} Completed");
49+
}
50+
51+
public void OnError(Exception error)
52+
{
53+
Console.WriteLine($"{observerName} Error: {error.Message}");
54+
}
55+
56+
public void OnNext(int value)
57+
{
58+
Console.WriteLine($"{observerName} Value: {value}");
59+
}
60+
}
61+
62+
#endregion

Advanced-C#-Programming/Advanced-C#-Programming.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "18_Custom-Collection-Initia
3939
EndProject
4040
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "19_Attributes", "19_Attributes\19_Attributes.csproj", "{F78E01F6-04B9-4941-B5CF-69013C4921E4}"
4141
EndProject
42-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "20_Reflection", "20_Reflection\20_Reflection.csproj", "{13B7EA4F-231D-4553-BEAC-6A439C1370D6}"
42+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "20_Reflection", "20_Reflection\20_Reflection.csproj", "{13B7EA4F-231D-4553-BEAC-6A439C1370D6}"
43+
EndProject
44+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "21_IObservable-IObserver", "21_IObservable-IObserver\21_IObservable-IObserver.csproj", "{B287980D-96C5-47DB-B46B-0A34ACB9C9E7}"
4345
EndProject
4446
Global
4547
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -123,6 +125,10 @@ Global
123125
{13B7EA4F-231D-4553-BEAC-6A439C1370D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
124126
{13B7EA4F-231D-4553-BEAC-6A439C1370D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
125127
{13B7EA4F-231D-4553-BEAC-6A439C1370D6}.Release|Any CPU.Build.0 = Release|Any CPU
128+
{B287980D-96C5-47DB-B46B-0A34ACB9C9E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
129+
{B287980D-96C5-47DB-B46B-0A34ACB9C9E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
130+
{B287980D-96C5-47DB-B46B-0A34ACB9C9E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
131+
{B287980D-96C5-47DB-B46B-0A34ACB9C9E7}.Release|Any CPU.Build.0 = Release|Any CPU
126132
EndGlobalSection
127133
GlobalSection(SolutionProperties) = preSolution
128134
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)