Skip to content

Commit bc434e6

Browse files
Fix signature for nullability (#10738)
1 parent d4500a5 commit bc434e6

File tree

6 files changed

+65
-60
lines changed

6 files changed

+65
-60
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<nullable>enable</nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ConsoleApplication1.Program1.Main();
2+
ConsoleApplication2.Program2.Main();
3+
ConsoleApplication3.Program3.Main();
4+
ConsoleApplication4.Program4.Main();

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
namespace ConsoleApplication1
55
{
6-
class Program
6+
public class Program1
77
{
8-
static void Main(string[] args)
8+
public static void Main()
99
{
10-
Counter c = new Counter(new Random().Next(10));
10+
Counter c = new(new Random().Next(10));
1111
c.ThresholdReached += c_ThresholdReached;
1212

1313
Console.WriteLine("press 'a' key to increase total");
@@ -18,7 +18,7 @@ static void Main(string[] args)
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);
@@ -27,33 +27,29 @@ static void c_ThresholdReached(object sender, EventArgs e)
2727

2828
class Counter
2929
{
30-
private int threshold;
31-
private int total;
30+
private readonly int _threshold;
31+
private int _total;
3232

3333
public Counter(int passedThreshold)
3434
{
35-
threshold = passedThreshold;
35+
_threshold = passedThreshold;
3636
}
3737

3838
public void Add(int x)
3939
{
40-
total += x;
41-
if (total >= threshold)
40+
_total += x;
41+
if (_total >= _threshold)
4242
{
4343
OnThresholdReached(EventArgs.Empty);
4444
}
4545
}
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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
using System;
22

3-
namespace ConsoleApplication1
3+
namespace ConsoleApplication2
44
{
55
// <snippet2>
6-
class Program
6+
public class Program2
77
{
8-
static void Main()
8+
public static void Main()
99
{
1010
var c = new Counter();
1111
c.ThresholdReached += c_ThresholdReached;
1212

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: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// <snippet6>
22
using System;
33

4-
namespace ConsoleApplication1
4+
namespace ConsoleApplication3
55
{
6-
class Program
6+
public class Program3
77
{
8-
static void Main(string[] args)
8+
public static void Main()
99
{
10-
Counter c = new Counter(new Random().Next(10));
10+
Counter c = new(new Random().Next(10));
1111
c.ThresholdReached += c_ThresholdReached;
1212

1313
Console.WriteLine("press 'a' key to increase total");
@@ -18,7 +18,7 @@ static void Main(string[] args)
1818
}
1919
}
2020

21-
static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
21+
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);
@@ -27,36 +27,34 @@ static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
2727

2828
class Counter
2929
{
30-
private int threshold;
31-
private int total;
30+
private readonly int _threshold;
31+
private int _total;
3232

3333
public Counter(int passedThreshold)
3434
{
35-
threshold = passedThreshold;
35+
_threshold = passedThreshold;
3636
}
3737

3838
public void Add(int x)
3939
{
40-
total += x;
41-
if (total >= threshold)
40+
_total += x;
41+
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
Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// <snippet7>
22
using System;
33

4-
namespace ConsoleApplication1
4+
namespace ConsoleApplication4
55
{
6-
class Program
6+
class Program4
77
{
8-
static void Main(string[] args)
8+
public static void Main()
99
{
10-
Counter c = new Counter(new Random().Next(10));
10+
Counter c = new(new Random().Next(10));
1111
c.ThresholdReached += c_ThresholdReached;
1212

1313
Console.WriteLine("press 'a' key to increase total");
@@ -18,7 +18,7 @@ static void Main(string[] args)
1818
}
1919
}
2020

21-
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);
@@ -27,36 +27,34 @@ static void c_ThresholdReached(Object sender, ThresholdReachedEventArgs e)
2727

2828
class Counter
2929
{
30-
private int threshold;
31-
private int total;
30+
private readonly int _threshold;
31+
private int _total;
3232

3333
public Counter(int passedThreshold)
3434
{
35-
threshold = passedThreshold;
35+
_threshold = passedThreshold;
3636
}
3737

3838
public void Add(int x)
3939
{
40-
total += x;
41-
if (total >= threshold)
40+
_total += x;
41+
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)