Created
May 15, 2012 11:31
-
-
Save clstopher/2701019 to your computer and use it in GitHub Desktop.
Truthy Class in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
public class MyClass | |
{ | |
// This class is just a quick spike to see what making C# "truthy" could look like. | |
// Don't use this in the real world without spending a bit of time making sure it doesn't break something! | |
public class MyNewBool | |
{ | |
public bool Value { get; set; } | |
public static implicit operator bool(MyNewBool val) | |
{ | |
return val != null && val.Value; | |
} | |
} | |
public static void RunSnippet() | |
{ | |
MyNewBool val = null; | |
WL(val ? "True" : "False"); // False | |
val = new MyNewBool() { Value = false }; | |
WL(val ? "True" : "False"); // False | |
val.Value = true; | |
WL(val ? "True" : "False"); // True | |
} | |
#region Helper methods | |
public static void Main() | |
{ | |
try | |
{ | |
RunSnippet(); | |
} | |
catch (Exception e) | |
{ | |
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString()); | |
Console.WriteLine(error); | |
} | |
finally | |
{ | |
Console.Write("Press any key to continue..."); | |
Console.ReadKey(); | |
} | |
} | |
private static void WL(object text, params object[] args) | |
{ | |
Console.WriteLine(text.ToString(), args); | |
} | |
private static void RL() | |
{ | |
Console.ReadLine(); | |
} | |
private static void Break() | |
{ | |
System.Diagnostics.Debugger.Break(); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment