using System.Diagnostics;
public static void Main()
var foo = new Foo { Bar = new Bar { Baz = "Hello" } };
var i = foo._(_=>_.Bar)._(_=>_.Baz);
Console.WriteLine("i is '{0}'", i);
var j = foo1._(_=>_.Bar)._(_=>_.Baz);
Console.WriteLine("j is '{0}'",j);
Console.WriteLine("js is null? {0}", j == null);
var x = s._(_=>_.Substring(0, 5));
Console.WriteLine("x is '{0}'", x);
var s1 = "Hello, World!";
var x1 = s1._(_=>_.Substring(0, 5));
Console.WriteLine("x1 is '{0}'", x1);
string s2 = default(string);
Console.WriteLine("s2 is null? {0}", s2 == null);
var x2 = s2._(_=>_.Trim());
Console.WriteLine("x2 is '{0}'", x2);
Console.WriteLine(x2._(_=>_.Trim()));
Console.WriteLine(x2.Trim());
Console.WriteLine("Can't call x2.Trim()! Exception: {0}", ex);
public Bar Bar { get; set; }
public string Baz { get; set; }
public static class ObjectExtension
public static K _<T, K>(this T o, Func<T, K> f)
catch(NullReferenceException nex)
Trace.TraceError(nex.ToString());