c# 6.0 Null Propogation Example

CSharp · Console

/Program.cs

using System;

public class Program
{
	public static void Main()
	{
		var foo = new Foo();
		Console.WriteLine("Null propagation");
		Console.WriteLine(foo.Bar?.Length);
		Console.WriteLine("Ternary");
		Console.WriteLine(foo.Bar != null ? foo.Bar.Length : (int ? )null);
	}
}

class Foo
{
	public string Bar
	{
		get
		{
			Console.WriteLine("I was read");
			return string.Empty;
		}
	}
}
;