Skip to content

Instantly share code, notes, and snippets.

@goenning
Last active June 23, 2017 12:25
Show Gist options
  • Save goenning/9c3e73fab143cc39948a38f8d33f6f1a to your computer and use it in GitHub Desktop.
Save goenning/9c3e73fab143cc39948a38f8d33f6f1a to your computer and use it in GitHub Desktop.
using System;
namespace ConsoleApp5
{
public class Sorting
{
public string Name { get; }
private Sorting(string name)
{
Name = name;
}
public static Sorting By(string name)
{
return new Sorting(name);
}
public static readonly Sorting Default = Sorting.By("Id");
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj is Sorting)
return this == (Sorting)obj;
return base.Equals(obj);
}
public static bool operator ==(Sorting a, Sorting b)
{
if (ReferenceEquals(a, null) && string.IsNullOrWhiteSpace(b.Name))
return true;
if (ReferenceEquals(b, null) && string.IsNullOrWhiteSpace(a.Name))
return true;
return string.Equals(a.Name, b.Name);
}
public static bool operator !=(Sorting a, Sorting b)
{
return !(a == b);
}
}
class Program
{
static void Main(string[] args)
{
var a = Sorting.By("Name") ?? Sorting.By("UploadedDate");
Console.WriteLine(a.Name);
var b = Sorting.Default ?? Sorting.By("UploadedDate");
Console.WriteLine(b.Name);
var c = null ?? Sorting.By("UploadedDate");
Console.WriteLine(c.Name);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment