72
1
using System;
2
using System.Diagnostics;
3
4
public class Program
5
{
6
public static void Main()
7
{
8
var foo = new Foo { Bar = new Bar { Baz = "Hello" } };
9
var foo1 = new Foo();
10
11
var i = foo._(_=>_.Bar)._(_=>_.Baz); //equivalent to foo?.Bar?.Baz in C# 6
12
Console.WriteLine("i is '{0}'", i); //should be 'Hello'
13
14
var j = foo1._(_=>_.Bar)._(_=>_.Baz); //equivalent to foo1?.Bar?.Baz in C# 6
15
Console.WriteLine("j is '{0}'",j); //should be '' since j is null, but shouldn't throw exception either.
16
17
Console.WriteLine("js is null? {0}", j == null);
18
19
string s = null;
20
var x = s._(_=>_.Substring(0, 5)); //calling string method via null propagation.
21
Console.WriteLine("x is '{0}'", x);
22
23
var s1 = "Hello, World!";
24
var x1 = s1._(_=>_.Substring(0, 5));
Cached Result