Skip to content

Instantly share code, notes, and snippets.

@bhargavrpatel
Last active August 29, 2015 13:57
Show Gist options
  • Save bhargavrpatel/9562545 to your computer and use it in GitHub Desktop.
Save bhargavrpatel/9562545 to your computer and use it in GitHub Desktop.
Something that would go inside your Models
/* CASE I: OBJECTS */
// BEFORE
public ViewResult CreateProduct() {
// create a new Product object
Product myProduct = new Product();
// set the property values
myProduct.ProductID = 100;
myProduct.Name = "Kayak";
myProduct.Description = "A boat for one person";
myProduct.Price = 275M;
myProduct.Category = "Watersports";
}
// AFTER (Using Initializers in C#)
Product myProduct = new Product {
ProductID = 100, Name = "Kayak", Description = "A boat for one person", Price = 275M, Category = "Watersports"
};
/* CASE II: COLLECTIONS */
// AFTER (Using Initializers in C#)
public ViewResult CreateCollection() {
string[] stringArray = { "apple", "orange", "plum" };
List<int> intList = new List<int> { 10, 20, 30, 40 };
Dictionary<string, int> myDict = new Dictionary<string, int> {{ "apple", 10 }, { "orange", 20 }, { "plum", 30 }};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment