Last active
December 14, 2016 14:43
-
-
Save josephwoodward/0db5f2d4116260a6e9b33a7cb16ce60c to your computer and use it in GitHub Desktop.
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
// 1: Object Initializer ////////////////////////////////// | |
public class User { | |
public bool IsActive { get; set; } | |
public string Name { get; set; } | |
} | |
var user = new User { | |
IsActive = true, | |
Name = "John Doe" | |
}; | |
// 2: Constructor ////////////////////////////////// | |
public class User { | |
public User(string name, bool isActive){ | |
Name = name; | |
IsActive = isActive; | |
} | |
public bool IsActive { get; private set; } | |
public string Name { get; private set; } | |
} | |
var user = new User("John Doe", true); // Boolean is confusing at this point? | |
// 3: Factory method ////////////////////////////////// | |
public class User { | |
public bool IsActive { get; private set; } | |
public string Name { get; private set; } | |
public static User CreateActiveUser(string name) => new User { | |
Name = name, | |
IsActive = true | |
}; | |
public static User CreateInactiveUser(string name) => new User { | |
Name = name, | |
IsActive = false | |
} | |
} | |
var user = User.CreateActiveUser("John Doe"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make the implicit explicit.
Or just use F#