Skip to content

Instantly share code, notes, and snippets.

@fagnercarvalho
Created December 28, 2015 02:16
Show Gist options
  • Select an option

  • Save fagnercarvalho/75e62ba490db8ea8f0f0 to your computer and use it in GitHub Desktop.

Select an option

Save fagnercarvalho/75e62ba490db8ea8f0f0 to your computer and use it in GitHub Desktop.
2014-10-26-Null-propagator-operator
using System;
using System.Collections.Generic;
namespace NullPropagator
{
public class Program
{
public static void Main(string[] args)
{
Bear b = new Bear { Claws = 2, Friends = new List<Bear>() { new Bear() } };
Console.WriteLine("Claws: " + b.Claws);
int? countFriends = b?.Friends?.Count;
if (countFriends.HasValue && countFriends > 0)
{
Console.WriteLine("Friends: " + countFriends);
}
else
{
Console.WriteLine("The bear has no friends.");
}
Console.Read();
}
}
public class Bear
{
public int Claws { get; set; }
public List<Bear> Friends { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment