Skip to content

Instantly share code, notes, and snippets.

View JayBazuzi's full-sized avatar

Jay Bazuzi JayBazuzi

View GitHub Profile
@JayBazuzi
JayBazuzi / async void event handler.cs
Created August 28, 2016 20:26
Demonstrating an alternative to `async void` event handlers. Both print out messages in the order listed (1/2/3/4). Also, if you remove the Sleep(), neither prints out #4 because the process exits first.
static void Main()
{
MyEvent += Handle;
Console.WriteLine("1. Before invoking event");
MyEvent(null, null);
Console.WriteLine("3. After invoking event");
Thread.Sleep(1000);
}
// Given:
var y = ...;
{
var x = F(y);
G(x);
}
// After
Action action = () => {
const int RetryAttempts = 5;
const int RetryIntervalSeconds = 8;
static FooResults FooClientExecuteCommand(FooClient client, string command)
{
FooResults results = null;
for (var i = 0; i <= RetryAttempts; i++)
{
try
{
return Win()
.DoIfNull(Advantage)
.DoIfNull(ScoreNormal)
.DoIfNull(ScorePerson1)
.DoIfNull(ScorePerson2)
.DoIfNull(ScoreTie);
@JayBazuzi
JayBazuzi / ifstructure.cs
Last active July 30, 2016 20:24 — forked from isidore/ifstructure.cs
If structure
string result = null;
if (result == null)
result = Win();
if (result == null)
result = Advantage();
if (result == null)
result = ScoreNormal();
if (result == null)
result = ScorePerson1();
if (result == null)
// A primitive
string emailAddress = "[email protected]";
// Better
class EmailAddress
{
public EmailAddress(string value) { this.Value = value; }
public string Value { get; }
}
class CustomerId
{
public CustomerId(string value)
{
this.Value = value;
}
public readonly string Value;
public override bool Equals(object obj)
class Point
{
public static bool operator==(Point left, Point right)
{
return left.Equals(right);
}
public static bool operator!=(Point left, Point right)
{
return left.Equals(right);
class Point
{
public static bool operator==(Point left, Point right)
{
return left.Equals(right);
}
}
class Point
{
override bool Equals(object other)
{
if (other.GetType() != this.GetType()) return false;
return this.X == ((Point)other).X && this.Y == ((Point)other).Y;
}
}