diff --git a/snippets/csharp/System/EventArgs/Overview/Project.csproj b/snippets/csharp/System/EventArgs/Overview/Project.csproj new file mode 100644 index 00000000000..086145b6424 --- /dev/null +++ b/snippets/csharp/System/EventArgs/Overview/Project.csproj @@ -0,0 +1,9 @@ + + + + Exe + net8.0 + enable + + + diff --git a/snippets/csharp/System/EventArgs/Overview/program.cs b/snippets/csharp/System/EventArgs/Overview/program.cs new file mode 100644 index 00000000000..292703bb57b --- /dev/null +++ b/snippets/csharp/System/EventArgs/Overview/program.cs @@ -0,0 +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 561fa2638a3..9c28189de8e 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"); @@ -18,7 +18,7 @@ static void Main(string[] args) } } - 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); @@ -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); } @@ -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 ac747b434e5..09a13d6947a 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; @@ -13,7 +13,7 @@ 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 3a49caa9e1b..7f93e9d112a 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() { - 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) + 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,36 +27,34 @@ 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.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 b063190ff94..aa69b44d6ba 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() { - 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,36 +27,34 @@ 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.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); } //