From d447e13cdf57d3c2ef5ccebebc880896081084c8 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Mon, 9 Dec 2024 10:17:06 +0100 Subject: [PATCH 1/3] fix signature for nullability and avoid warning --- snippets/csharp/System/EventArgs/Overview/programwithdata.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/csharp/System/EventArgs/Overview/programwithdata.cs b/snippets/csharp/System/EventArgs/Overview/programwithdata.cs index 3a49caa9e1b..e0616f0fcfb 100644 --- a/snippets/csharp/System/EventArgs/Overview/programwithdata.cs +++ b/snippets/csharp/System/EventArgs/Overview/programwithdata.cs @@ -18,7 +18,7 @@ static void Main(string[] args) } } - static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e) + static void c_ThresholdReached(object? sender, ThresholdReachedEventArgs e) { Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached); Environment.Exit(0); From 08ddb0bfdfb89e7d6b01c27fde053d4b5402bc4e Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Mon, 9 Dec 2024 10:33:59 -0800 Subject: [PATCH 2/3] add project file and make it compile --- .../System/EventArgs/Overview/Project.csproj | 8 +++++++ .../System/EventArgs/Overview/program.cs | 1 + .../EventArgs/Overview/programnodata.cs | 16 +++++++------- .../EventArgs/Overview/programtruncated.cs | 6 ++--- .../EventArgs/Overview/programwithdata.cs | 20 ++++++++--------- .../EventArgs/Overview/programwithdelegate.cs | 22 +++++++++---------- 6 files changed, 41 insertions(+), 32 deletions(-) create mode 100644 snippets/csharp/System/EventArgs/Overview/Project.csproj create mode 100644 snippets/csharp/System/EventArgs/Overview/program.cs diff --git a/snippets/csharp/System/EventArgs/Overview/Project.csproj b/snippets/csharp/System/EventArgs/Overview/Project.csproj new file mode 100644 index 00000000000..a269962b552 --- /dev/null +++ b/snippets/csharp/System/EventArgs/Overview/Project.csproj @@ -0,0 +1,8 @@ + + + + Exe + net8.0 + + + diff --git a/snippets/csharp/System/EventArgs/Overview/program.cs b/snippets/csharp/System/EventArgs/Overview/program.cs new file mode 100644 index 00000000000..15c60010aae --- /dev/null +++ b/snippets/csharp/System/EventArgs/Overview/program.cs @@ -0,0 +1 @@ +ConsoleApplication1.Program1.Main(); diff --git a/snippets/csharp/System/EventArgs/Overview/programnodata.cs b/snippets/csharp/System/EventArgs/Overview/programnodata.cs index 561fa2638a3..b21a04cff7b 100644 --- a/snippets/csharp/System/EventArgs/Overview/programnodata.cs +++ b/snippets/csharp/System/EventArgs/Overview/programnodata.cs @@ -3,11 +3,11 @@ namespace ConsoleApplication1 { - class Program + public class Program1 { - static void Main(string[] args) + public static void Main() { - Counter c = new Counter(new Random().Next(10)); + Counter c = new(new Random().Next(10)); c.ThresholdReached += c_ThresholdReached; Console.WriteLine("press 'a' key to increase total"); @@ -27,18 +27,18 @@ static void c_ThresholdReached(object sender, EventArgs e) class Counter { - private int threshold; - private int total; + private readonly int _threshold; + private int _total; public Counter(int passedThreshold) { - threshold = passedThreshold; + _threshold = passedThreshold; } public void Add(int x) { - total += x; - if (total >= threshold) + _total += x; + if (_total >= _threshold) { OnThresholdReached(EventArgs.Empty); } diff --git a/snippets/csharp/System/EventArgs/Overview/programtruncated.cs b/snippets/csharp/System/EventArgs/Overview/programtruncated.cs index ac747b434e5..ea52e62e2bb 100644 --- a/snippets/csharp/System/EventArgs/Overview/programtruncated.cs +++ b/snippets/csharp/System/EventArgs/Overview/programtruncated.cs @@ -1,11 +1,11 @@ using System; -namespace ConsoleApplication1 +namespace ConsoleApplication2 { // - class Program + public class Program2 { - static void Main() + public static void Main() { var c = new Counter(); c.ThresholdReached += c_ThresholdReached; diff --git a/snippets/csharp/System/EventArgs/Overview/programwithdata.cs b/snippets/csharp/System/EventArgs/Overview/programwithdata.cs index e0616f0fcfb..c3ae03928e0 100644 --- a/snippets/csharp/System/EventArgs/Overview/programwithdata.cs +++ b/snippets/csharp/System/EventArgs/Overview/programwithdata.cs @@ -1,13 +1,13 @@ // using System; -namespace ConsoleApplication1 +namespace ConsoleApplication3 { - class Program + public class Program3 { - static void Main(string[] args) + public static void Main(string[] args) { - Counter c = new Counter(new Random().Next(10)); + Counter c = new(new Random().Next(10)); c.ThresholdReached += c_ThresholdReached; Console.WriteLine("press 'a' key to increase total"); @@ -27,21 +27,21 @@ static void c_ThresholdReached(object? sender, ThresholdReachedEventArgs e) class Counter { - private int threshold; - private int total; + private readonly int _threshold; + private int _total; public Counter(int passedThreshold) { - threshold = passedThreshold; + _threshold = passedThreshold; } public void Add(int x) { - total += x; - if (total >= threshold) + _total += x; + if (_total >= _threshold) { ThresholdReachedEventArgs args = new ThresholdReachedEventArgs(); - args.Threshold = threshold; + args.Threshold = _threshold; args.TimeReached = DateTime.Now; OnThresholdReached(args); } diff --git a/snippets/csharp/System/EventArgs/Overview/programwithdelegate.cs b/snippets/csharp/System/EventArgs/Overview/programwithdelegate.cs index b063190ff94..cd9a545cc90 100644 --- a/snippets/csharp/System/EventArgs/Overview/programwithdelegate.cs +++ b/snippets/csharp/System/EventArgs/Overview/programwithdelegate.cs @@ -1,13 +1,13 @@ // using System; -namespace ConsoleApplication1 +namespace ConsoleApplication4 { - class Program + class Program4 { - static void Main(string[] args) + public static void Main(string[] args) { - Counter c = new Counter(new Random().Next(10)); + Counter c = new(new Random().Next(10)); c.ThresholdReached += c_ThresholdReached; Console.WriteLine("press 'a' key to increase total"); @@ -18,7 +18,7 @@ static void Main(string[] args) } } - static void c_ThresholdReached(Object sender, ThresholdReachedEventArgs e) + public static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e) { Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached); Environment.Exit(0); @@ -27,21 +27,21 @@ static void c_ThresholdReached(Object sender, ThresholdReachedEventArgs e) class Counter { - private int threshold; - private int total; + private readonly int _threshold; + private int _total; public Counter(int passedThreshold) { - threshold = passedThreshold; + _threshold = passedThreshold; } public void Add(int x) { - total += x; - if (total >= threshold) + _total += x; + if (_total >= _threshold) { ThresholdReachedEventArgs args = new ThresholdReachedEventArgs(); - args.Threshold = threshold; + args.Threshold = _threshold; args.TimeReached = DateTime.Now; OnThresholdReached(args); } From 354839bcd9be36b9b29fa3a006304ea16565973e Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Mon, 9 Dec 2024 10:42:03 -0800 Subject: [PATCH 3/3] a few more updates --- .../System/EventArgs/Overview/Project.csproj | 1 + .../System/EventArgs/Overview/program.cs | 3 +++ .../EventArgs/Overview/programnodata.cs | 10 +++------ .../EventArgs/Overview/programtruncated.cs | 6 ++--- .../EventArgs/Overview/programwithdata.cs | 18 +++++++-------- .../EventArgs/Overview/programwithdelegate.cs | 22 +++++++++---------- 6 files changed, 28 insertions(+), 32 deletions(-) diff --git a/snippets/csharp/System/EventArgs/Overview/Project.csproj b/snippets/csharp/System/EventArgs/Overview/Project.csproj index a269962b552..086145b6424 100644 --- a/snippets/csharp/System/EventArgs/Overview/Project.csproj +++ b/snippets/csharp/System/EventArgs/Overview/Project.csproj @@ -3,6 +3,7 @@ Exe net8.0 + enable diff --git a/snippets/csharp/System/EventArgs/Overview/program.cs b/snippets/csharp/System/EventArgs/Overview/program.cs index 15c60010aae..292703bb57b 100644 --- a/snippets/csharp/System/EventArgs/Overview/program.cs +++ b/snippets/csharp/System/EventArgs/Overview/program.cs @@ -1 +1,4 @@ ConsoleApplication1.Program1.Main(); +ConsoleApplication2.Program2.Main(); +ConsoleApplication3.Program3.Main(); +ConsoleApplication4.Program4.Main(); diff --git a/snippets/csharp/System/EventArgs/Overview/programnodata.cs b/snippets/csharp/System/EventArgs/Overview/programnodata.cs index b21a04cff7b..9c28189de8e 100644 --- a/snippets/csharp/System/EventArgs/Overview/programnodata.cs +++ b/snippets/csharp/System/EventArgs/Overview/programnodata.cs @@ -18,7 +18,7 @@ public static void Main() } } - static void c_ThresholdReached(object sender, EventArgs e) + static void c_ThresholdReached(object? sender, EventArgs e) { Console.WriteLine("The threshold was reached."); Environment.Exit(0); @@ -46,14 +46,10 @@ public void Add(int x) protected virtual void OnThresholdReached(EventArgs e) { - EventHandler handler = ThresholdReached; - if (handler != null) - { - handler(this, e); - } + ThresholdReached?.Invoke(this, e); } - public event EventHandler ThresholdReached; + public event EventHandler? ThresholdReached; } } // diff --git a/snippets/csharp/System/EventArgs/Overview/programtruncated.cs b/snippets/csharp/System/EventArgs/Overview/programtruncated.cs index ea52e62e2bb..09a13d6947a 100644 --- a/snippets/csharp/System/EventArgs/Overview/programtruncated.cs +++ b/snippets/csharp/System/EventArgs/Overview/programtruncated.cs @@ -13,7 +13,7 @@ public static void Main() // provide remaining implementation for the class } - static void c_ThresholdReached(object sender, EventArgs e) + static void c_ThresholdReached(object? sender, EventArgs e) { Console.WriteLine("The threshold was reached."); } @@ -23,11 +23,11 @@ static void c_ThresholdReached(object sender, EventArgs e) // class Counter { - public event EventHandler ThresholdReached; + public event EventHandler? ThresholdReached; protected virtual void OnThresholdReached(EventArgs e) { - EventHandler handler = ThresholdReached; + EventHandler? handler = ThresholdReached; handler?.Invoke(this, e); } diff --git a/snippets/csharp/System/EventArgs/Overview/programwithdata.cs b/snippets/csharp/System/EventArgs/Overview/programwithdata.cs index c3ae03928e0..7f93e9d112a 100644 --- a/snippets/csharp/System/EventArgs/Overview/programwithdata.cs +++ b/snippets/csharp/System/EventArgs/Overview/programwithdata.cs @@ -5,7 +5,7 @@ namespace ConsoleApplication3 { public class Program3 { - public static void Main(string[] args) + public static void Main() { Counter c = new(new Random().Next(10)); c.ThresholdReached += c_ThresholdReached; @@ -40,23 +40,21 @@ public void Add(int x) _total += x; if (_total >= _threshold) { - ThresholdReachedEventArgs args = new ThresholdReachedEventArgs(); - args.Threshold = _threshold; - args.TimeReached = DateTime.Now; + ThresholdReachedEventArgs args = new() + { + Threshold = _threshold, + TimeReached = DateTime.Now + }; OnThresholdReached(args); } } protected virtual void OnThresholdReached(ThresholdReachedEventArgs e) { - EventHandler handler = ThresholdReached; - if (handler != null) - { - handler(this, e); - } + ThresholdReached?.Invoke(this, e); } - public event EventHandler ThresholdReached; + public event EventHandler? ThresholdReached; } public class ThresholdReachedEventArgs : EventArgs diff --git a/snippets/csharp/System/EventArgs/Overview/programwithdelegate.cs b/snippets/csharp/System/EventArgs/Overview/programwithdelegate.cs index cd9a545cc90..aa69b44d6ba 100644 --- a/snippets/csharp/System/EventArgs/Overview/programwithdelegate.cs +++ b/snippets/csharp/System/EventArgs/Overview/programwithdelegate.cs @@ -5,7 +5,7 @@ namespace ConsoleApplication4 { class Program4 { - public static void Main(string[] args) + public static void Main() { Counter c = new(new Random().Next(10)); c.ThresholdReached += c_ThresholdReached; @@ -18,7 +18,7 @@ public static void Main(string[] args) } } - public static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e) + public static void c_ThresholdReached(object? sender, ThresholdReachedEventArgs e) { Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached); Environment.Exit(0); @@ -40,23 +40,21 @@ public void Add(int x) _total += x; if (_total >= _threshold) { - ThresholdReachedEventArgs args = new ThresholdReachedEventArgs(); - args.Threshold = _threshold; - args.TimeReached = DateTime.Now; + ThresholdReachedEventArgs args = new() + { + Threshold = _threshold, + TimeReached = DateTime.Now + }; OnThresholdReached(args); } } protected virtual void OnThresholdReached(ThresholdReachedEventArgs e) { - ThresholdReachedEventHandler handler = ThresholdReached; - if (handler != null) - { - handler(this, e); - } + ThresholdReached?.Invoke(this, e); } - public event ThresholdReachedEventHandler ThresholdReached; + public event ThresholdReachedEventHandler? ThresholdReached; } public class ThresholdReachedEventArgs : EventArgs @@ -65,6 +63,6 @@ public class ThresholdReachedEventArgs : EventArgs public DateTime TimeReached { get; set; } } - public delegate void ThresholdReachedEventHandler(Object sender, ThresholdReachedEventArgs e); + public delegate void ThresholdReachedEventHandler(object sender, ThresholdReachedEventArgs e); } //