C
C#3y ago
nekodjin

Object Initializers - To Parenthesize or Not To Parenthesize (that is the question)

What is the difference between the object initializers on lines 1 and 2? In what situations should one be preferred over the other?
8 Replies
Tvde1
Tvde13y ago
There is no difference, the () can be omitted if you have an object initializer
nekodjin
nekodjinOP3y ago
why does the second one exist
Tvde1
Tvde13y ago
¯\_(ツ)_/¯
nekodjin
nekodjinOP3y ago
lmao can you put parameters in it? arguments *
Tvde1
Tvde13y ago
If there is a constructor that does require parameters, you can still call it (and have an initializer after it)
nekodjin
nekodjinOP3y ago
ah i see so the second one is just there for consistency i guess and the first is a shorthand ?
Tvde1
Tvde13y ago
yeah I suppose I'm also curious what the reasoning is for allowing () to be omitted
nekodjin
nekodjinOP3y ago
i personally prefer it without the () so i guess it's just a pleasant shorthand ajyway tnaks thanks :))
Want results from more Discord servers?
Add your server