Skip to content

Instantly share code, notes, and snippets.

@jmreynolds
Last active February 2, 2016 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmreynolds/971b7e715b153cc9297b to your computer and use it in GitHub Desktop.
Save jmreynolds/971b7e715b153cc9297b to your computer and use it in GitHub Desktop.
C# 6 Features: Auto-Property Initializer Examples
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}");
}
}
public class Person
{
public Guid PersonId { get; set; } = Guid.NewGuid();
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Person
{
public Guid PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Person()
{
PersonId = Guid.NewGuid();
}
}
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