-1

C# Initialize Object With Properties of Another Instance

(A) I can do this...

var newRestaurant = new Restaurant();
newRestaurant.Cuisine = model.Cuisine;
newRestaurant.Name = model.Name;

(B) And I can write it this way...

var newRestaurant = new Restaurant() { Name = model.Name };

(C) But how come I can't write it like so...

var newRestaurant = new Restaurant() model;

(Q) Isn't (B) just an Object-Literal while (C) is an object instance?

Would love to know.

Thx

Cody
  • 9,785
  • 4
  • 61
  • 46
  • 4
    You'll have to ask MS why they haven't added this feature yet. We can't possibly answer why they haven't chosen to add any particular feature to the language. – Servy Dec 09 '16 at 18:14
  • 1
    why not create a method with `System.Reflection` to populate a object with the values of another, and put this in a constructor that receive a object of the same type as param? – Lucas Dec 09 '16 at 18:14
  • (B) is an object initializer and is just syntactic sugar for `var newRestaurant = new Restaurant(); newRestaurant.Name = model.Name;` – itsme86 Dec 09 '16 at 18:14
  • 1
    @itsme86 Almost, there's actually a temp variable involved as the assignment to `Name` will occur before the assignment to `newRestaurant`. – juharr Dec 09 '16 at 18:17
  • Got it, thx guys. I'm a bit of a greenhorns (obviously) with C# -- this helps a lot. – Cody Dec 09 '16 at 18:22

1 Answers1

4

Isn't (B) just an Object-Literal while (C) is an object instance?

The short answer? No. C# doesn't use curly braces to represent object literals like JavaScript or similar languages do; C# uses curly braces to refer to blocks.

In the code

var newRestaurant = new Restaurant() { Name = model.Name };

the { Name = model.Name } part isn't an object literal, it's an initializer block. You can use a similar syntax to initialize collections like lists and dictionaries:

var myString = "string3";
var myList = new List<string>() { "string1", "string2", myString };
var myDictionary = new Dictionary<string, int>() 
{
    { "string1", 1 },
    { "string2", 2 },
    { myString, 3 },
};

As you can see, the syntax of these blocks differs based on what sort of object is before them. This code is transformed by the compiler into

var myString = "string3";
var myList = new List<string>();
myList.Add("string1");
myList.Add("string2");
myList.Add(myString);
var myDictionary = new Dictionary<string, int>();
myDictionary.Add("string1", 1);
myDictionary.Add("string2", 2);
myDictionary.Add(myString, 3);

And in the case of your example, it's transformed into:

var newRestaurant = new Restaurant(); 
newRestaurant.Name = model.Name;

When you try and use model like var newRestaurant = new Restaurant() model;, the compiler has no idea what sorts of properties are in model or what you meant to do with them; are you trying to add to a list? Are you trying to copy all the properties? What does it do if all the properties in model don't match?

Further reading

Later versions of C# will have something called Records, which will have a similar feature to what you're describing (copying fields from one thing to another). You can read about them on the compiler's github page if you're interested, but it's pretty technical.

Alex Van Liew
  • 1,339
  • 9
  • 15
  • That's an awesome answer -- thx! – Cody Dec 09 '16 at 18:26
  • 1
    No problem. C# has a lot of awesome syntactic sugar - shorthands for things you could say in a longer way, but there's no reason to. It's one of the reasons it's up at the top of my list of favorite languages to work in, but it's been built up over time so it can be a little overwhelming for new users if they aren't exposed to the "unsweetened" way first. – Alex Van Liew Dec 09 '16 at 18:28
  • Thanks for the JS comment -- I cut my teeth on JavaScript, but in addition to some other languages, I've done some ad hoc, crash course-ish sort of development in the past. I finally dug my heels in and got serious about the fundamentals of C#. C# is proving itself very worthy of my attention, learning and time. Thanks a ton. – Cody Dec 09 '16 at 18:35
  • 1
    I kind of figured; JS is the most common language that uses braces for object literals (which is a bit of a shame given that braces also have a completely separate, disparate meaning in JS). Getting hung up on syntax is the worst; lots of languages have lots of ways of writing the same thing, and sometimes the syntax of a piece of code in one language means something totally different in another. – Alex Van Liew Dec 09 '16 at 18:52