Last active
February 2, 2016 00:21
-
-
Save jmreynolds/971b7e715b153cc9297b to your computer and use it in GitHub Desktop.
C# 6 Features: Auto-Property Initializer Examples
This file contains 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
public class Person | |
{ | |
public Guid PersonId { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
} | |
public class Program | |
{ | |
public static void Main() | |
{ | |
var person = new Person | |
{ | |
FirstName = "Joe", | |
LastName = "Reynolds" | |
}; | |
if(person.PersonId == Guid.Empty) Console.WriteLine("PersonId was not set"); | |
else Console.WriteLine($"New Person ID: {person.PersonId}"); | |
Console.WriteLine($"New Name: {person.FirstName} {person.LastName}"); | |
} | |
} |
This file contains 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
public class Person | |
{ | |
public Guid PersonId { get; set; } = Guid.NewGuid(); | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
} |
This file contains 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
public class Person | |
{ | |
public Guid PersonId { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public Person() | |
{ | |
PersonId = Guid.NewGuid(); | |
} | |
} |
This file contains 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
public static void Main() | |
{ | |
var person = new Person | |
{ | |
FirstName = "Joe", | |
LastName = "Reynolds", | |
PersonId = Guid.NewGuid() | |
}; | |
// snip | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment