Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions snippets/csharp/System/EventArgs/Overview/Project.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<nullable>enable</nullable>
</PropertyGroup>

</Project>
4 changes: 4 additions & 0 deletions snippets/csharp/System/EventArgs/Overview/program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ConsoleApplication1.Program1.Main();
ConsoleApplication2.Program2.Main();
ConsoleApplication3.Program3.Main();
ConsoleApplication4.Program4.Main();
26 changes: 11 additions & 15 deletions snippets/csharp/System/EventArgs/Overview/programnodata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand All @@ -27,33 +27,29 @@ 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);
}
}

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;
}
}
// </snippet5>
12 changes: 6 additions & 6 deletions snippets/csharp/System/EventArgs/Overview/programtruncated.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using System;

namespace ConsoleApplication1
namespace ConsoleApplication2
{
// <snippet2>
class Program
public class Program2
{
static void Main()
public static void Main()
{
var c = new Counter();
c.ThresholdReached += c_ThresholdReached;

// 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.");
}
Expand All @@ -23,11 +23,11 @@ static void c_ThresholdReached(object sender, EventArgs e)
// <snippet1>
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);
}

Expand Down
36 changes: 17 additions & 19 deletions snippets/csharp/System/EventArgs/Overview/programwithdata.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// <snippet6>
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");
Expand All @@ -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);
Expand All @@ -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<ThresholdReachedEventArgs> handler = ThresholdReached;
if (handler != null)
{
handler(this, e);
}
ThresholdReached?.Invoke(this, e);
}

public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
public event EventHandler<ThresholdReachedEventArgs>? ThresholdReached;
}

public class ThresholdReachedEventArgs : EventArgs
Expand Down
38 changes: 18 additions & 20 deletions snippets/csharp/System/EventArgs/Overview/programwithdelegate.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// <snippet7>
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");
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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);
}
// </snippet7>
Loading