Snippets for http://fagner.co/2014/10/26/Null-propagator-operator/
Created
December 28, 2015 02:16
-
-
Save fagnercarvalho/75e62ba490db8ea8f0f0 to your computer and use it in GitHub Desktop.
2014-10-26-Null-propagator-operator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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