14

I'm writing a small example to practice creating new instances of a class.

I have the following code:

class Person
{
   public string Name { get; set; }
   public int Age { get; set; }
}

class MainClass
{
   static void Main()
   {
      var p = new Person
      {
         Name = "Harry",
         Age = 20
      };
      Console.WriteLine($"Name: {p.Name}. Age: {p.Age}");

      p = new Person()
      {
         Name = "Hermonie",
         Age = 18
      };
      Console.WriteLine($"Name: {p.Name}. Age: {p.Age}");

      Console.ReadLine();
   }
}

It's working.

My question: What's the difference between

var p = new Person {};

and

var p = new Person() {};

Which version should I use?

Jeroen
  • 60,696
  • 40
  • 206
  • 339
Tân
  • 1
  • 15
  • 56
  • 102
  • 2
    see: http://stackoverflow.com/questions/3661025/why-are-c-sharp-3-0-object-initializer-constructor-parentheses-optional – Taha Paksu Jan 11 '16 at 10:27
  • 1
    Normally one would omit the `()` part, if you are using the default constructor. – Matthew Watson Jan 11 '16 at 10:27
  • 3
    Off Topic: I know Hermione is academically brilliant and all, is there anywhere in the books that shows she's been put ahead a year, as alluded by the ages in the question? :) – James Thorpe Jan 11 '16 at 10:27
  • @JamesThorpe Magic. haha :) – Tân Jan 11 '16 at 10:36
  • 1
    @HappyCoding Actually re-reading them on and off at the moment as my main memories of the story are the films now - _so_ much more in the books (as always). Don't recall anything specific about ages either way, would have to go and look :) – James Thorpe Jan 11 '16 at 10:38
  • Keep in mind that she's also gained a bit of age, relative to her friends, due to her use of the time turner. – William Aug 16 '17 at 17:19

4 Answers4

12

Both will call the default parameter-less constructor. So I believe both are same.

Anup Sharma
  • 2,053
  • 20
  • 30
4

In this case there is no difference, they both call the default constructor. The difference would be obvious if there was another constructor with parameters:

var o = new Person { ... };
var p = new Person("John") { ... };

The empty parentheses are only needed when you don't use the initialization:

var p = new Person(); // Works
var o = new Person; // Error
Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
2

Unless you wanted to initialize the property values, using the standard:

Person p = new Person();

Should suffice, but they are the same thing in your case and call the default constructor.

But, if you wanted to set the property values, you can do the following:

Person p = new Person { Name = "Harry", Age = 18 };
Ric
  • 12,855
  • 3
  • 30
  • 36
1

Declare a p by using the default constructor:

var p = new Person(); 

Declare a p by using a constructor that takes name as a parameter:

var p = new Person("Harry"); 

Declare a p by using an object initialiser:

var p = new Person {Name="Harry"};

See What's the difference between an object initializer and a constructor? and https://msdn.microsoft.com/en-us/library/bb397680.aspx

Community
  • 1
  • 1