Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between new Object{} and new Object(){} [duplicate]

Tags:

c#

I define objects from class in C# this way

var something = new SomeThing()
{
    Property = "SomeProperty"
}

and this way

var something = new SomeThing
{
    Property = "SomeProperty"
}

what is the difference between these definitions?

like image 652
aikutto Avatar asked Dec 03 '22 20:12

aikutto


2 Answers

Nothing. The () is redundant. The only thing required is that the object has a default constructor.

like image 200
DaveDev Avatar answered Dec 05 '22 10:12

DaveDev


Nothing, when the parentheses are empty.

They only make sense when you provide a parameter; you can do that at the same time:

var something = new SomeThing("some other value")
{
    Property = "SomeProperty"
}
like image 23
Kjartan Avatar answered Dec 05 '22 10:12

Kjartan