Setting properties via object initialization or not : Any difference ?

前端 未结 5 734
别跟我提以往
别跟我提以往 2020-12-09 07:43

Here\'s a simple question : Is there any (performance) difference between this :

Person person = new Person()
{
  Name = \"Philippe\",
  Mail = \"phil@phil.c         


        
5条回答
  •  离开以前
    2020-12-09 08:12

    Performance-wise there is no significant difference, as other replies have shown.

    However, creating an object using an initializer with 2 parameters seems to me like you state your intent to anyone who is using it, forming a "contract" saying: "these 2 parameters are the minimum for the functionality of the class" (although the correct way to express that intent would be to use a constructor).

    I tend to think of the initializer syntax this way, although it's more or less just syntactic sugar. I use a mix of both syntaxes in my code. But then again, that's my personal style.

提交回复
热议问题