Skip to content

Commit 354839b

Browse files
committed
a few more updates
1 parent 08ddb0b commit 354839b

File tree

6 files changed

+28
-32
lines changed

6 files changed

+28
-32
lines changed

snippets/csharp/System/EventArgs/Overview/Project.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net8.0</TargetFramework>
6+
<nullable>enable</nullable>
67
</PropertyGroup>
78

89
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
ConsoleApplication1.Program1.Main();
2+
ConsoleApplication2.Program2.Main();
3+
ConsoleApplication3.Program3.Main();
4+
ConsoleApplication4.Program4.Main();

snippets/csharp/System/EventArgs/Overview/programnodata.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static void Main()
1818
}
1919
}
2020

21-
static void c_ThresholdReached(object sender, EventArgs e)
21+
static void c_ThresholdReached(object? sender, EventArgs e)
2222
{
2323
Console.WriteLine("The threshold was reached.");
2424
Environment.Exit(0);
@@ -46,14 +46,10 @@ public void Add(int x)
4646

4747
protected virtual void OnThresholdReached(EventArgs e)
4848
{
49-
EventHandler handler = ThresholdReached;
50-
if (handler != null)
51-
{
52-
handler(this, e);
53-
}
49+
ThresholdReached?.Invoke(this, e);
5450
}
5551

56-
public event EventHandler ThresholdReached;
52+
public event EventHandler? ThresholdReached;
5753
}
5854
}
5955
// </snippet5>

snippets/csharp/System/EventArgs/Overview/programtruncated.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static void Main()
1313
// provide remaining implementation for the class
1414
}
1515

16-
static void c_ThresholdReached(object sender, EventArgs e)
16+
static void c_ThresholdReached(object? sender, EventArgs e)
1717
{
1818
Console.WriteLine("The threshold was reached.");
1919
}
@@ -23,11 +23,11 @@ static void c_ThresholdReached(object sender, EventArgs e)
2323
// <snippet1>
2424
class Counter
2525
{
26-
public event EventHandler ThresholdReached;
26+
public event EventHandler? ThresholdReached;
2727

2828
protected virtual void OnThresholdReached(EventArgs e)
2929
{
30-
EventHandler handler = ThresholdReached;
30+
EventHandler? handler = ThresholdReached;
3131
handler?.Invoke(this, e);
3232
}
3333

snippets/csharp/System/EventArgs/Overview/programwithdata.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ConsoleApplication3
55
{
66
public class Program3
77
{
8-
public static void Main(string[] args)
8+
public static void Main()
99
{
1010
Counter c = new(new Random().Next(10));
1111
c.ThresholdReached += c_ThresholdReached;
@@ -40,23 +40,21 @@ public void Add(int x)
4040
_total += x;
4141
if (_total >= _threshold)
4242
{
43-
ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
44-
args.Threshold = _threshold;
45-
args.TimeReached = DateTime.Now;
43+
ThresholdReachedEventArgs args = new()
44+
{
45+
Threshold = _threshold,
46+
TimeReached = DateTime.Now
47+
};
4648
OnThresholdReached(args);
4749
}
4850
}
4951

5052
protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
5153
{
52-
EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
53-
if (handler != null)
54-
{
55-
handler(this, e);
56-
}
54+
ThresholdReached?.Invoke(this, e);
5755
}
5856

59-
public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
57+
public event EventHandler<ThresholdReachedEventArgs>? ThresholdReached;
6058
}
6159

6260
public class ThresholdReachedEventArgs : EventArgs

snippets/csharp/System/EventArgs/Overview/programwithdelegate.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ConsoleApplication4
55
{
66
class Program4
77
{
8-
public static void Main(string[] args)
8+
public static void Main()
99
{
1010
Counter c = new(new Random().Next(10));
1111
c.ThresholdReached += c_ThresholdReached;
@@ -18,7 +18,7 @@ public static void Main(string[] args)
1818
}
1919
}
2020

21-
public static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
21+
public static void c_ThresholdReached(object? sender, ThresholdReachedEventArgs e)
2222
{
2323
Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);
2424
Environment.Exit(0);
@@ -40,23 +40,21 @@ public void Add(int x)
4040
_total += x;
4141
if (_total >= _threshold)
4242
{
43-
ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
44-
args.Threshold = _threshold;
45-
args.TimeReached = DateTime.Now;
43+
ThresholdReachedEventArgs args = new()
44+
{
45+
Threshold = _threshold,
46+
TimeReached = DateTime.Now
47+
};
4648
OnThresholdReached(args);
4749
}
4850
}
4951

5052
protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
5153
{
52-
ThresholdReachedEventHandler handler = ThresholdReached;
53-
if (handler != null)
54-
{
55-
handler(this, e);
56-
}
54+
ThresholdReached?.Invoke(this, e);
5755
}
5856

59-
public event ThresholdReachedEventHandler ThresholdReached;
57+
public event ThresholdReachedEventHandler? ThresholdReached;
6058
}
6159

6260
public class ThresholdReachedEventArgs : EventArgs
@@ -65,6 +63,6 @@ public class ThresholdReachedEventArgs : EventArgs
6563
public DateTime TimeReached { get; set; }
6664
}
6765

68-
public delegate void ThresholdReachedEventHandler(Object sender, ThresholdReachedEventArgs e);
66+
public delegate void ThresholdReachedEventHandler(object sender, ThresholdReachedEventArgs e);
6967
}
7068
// </snippet7>

0 commit comments

Comments
 (0)