Skip to content

Instantly share code, notes, and snippets.

@clstopher
Created May 15, 2012 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clstopher/2701019 to your computer and use it in GitHub Desktop.
Save clstopher/2701019 to your computer and use it in GitHub Desktop.
Truthy Class in C#
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