Setting properties via object initialization or not : Any difference ?

前端 未结 5 741
别跟我提以往
别跟我提以往 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:06

    As others have said, no, there is no difference. Note that the first example isn't actually using a constructor for those arguments. It's using the "object initializer" language feature introduced in C# 3.0. The constructor being called is the default parameterless constructor just like the second example.

    The two examples actually compile down to the same IL code and do exactly the same thing. The first example is just syntactic sugar to accomplish the task in an easier and more expressive way.

提交回复
热议问题