Search code examples
c#-3.0automatic-properties

Unexpected C# behaviour with autoproperties and constructors


It took me some debugging to figure this out (or so do I think). I will let the code loose on you and see what you come up with. There is a simple Contact class with:

  1. Some auto-properties,
  2. A parameterized constructor which always increments the Contact.ID property and sets other properties according to the arguments it gets
  3. A parameterless constructor which always calls the parameterized constructor with default values.

First see the code; its output and the question follows the code:

 using System;

 class Program 
 {
   private static void Main(string[] args) 
   {
      Contact[] contacts_array = {

         //Contact 0
         new Contact(),

         //Contact 1
         new Contact {
            Name = "contactName1",
            Age = 40,
            Email = "[email protected]"
         },

         //Contact 2
         new Contact {
            Name = "contactName2",
            Age = 41,
            Email = "[email protected]"
         },

         //Contact 3
         new Contact("contactName3",
            42,
            "[email protected]"),
      };

      foreach (var contact in contacts_array)
         Console.WriteLine(contact);

      Console.ReadLine();
   }
}

public class Contact
{
    public static int totalContacts = 0;
    public int Id { get; private set; }
    public string Name { get; set; }
    public int? Age { get; set; }
    public string Email { get; set; }

    public Contact()
    {  
        new Contact("ANONYMOUS", null, "[email protected]");  
    }

    public Contact(string name, int? age, string email)
    {
        Id = Contact.totalContacts++;
        Name = name;
        Age = age;
        Email = email;
    }

    public override string ToString()
    {
        return string.Format("[Contact: Id={0}, Name={1}, Age={2}, Email={3}]",
                             Id, Name, Age, Email);
    }
}

Output:

[Contact: Id=0, Name=, Age=, Email=]  
[Contact: Id=0, Name=contactName1, Age=40, [email protected]]  
[Contact: Id=0, Name=contactName2, Age=41, [email protected]]  
[Contact: Id=3, Name=contactName3, Age=42, [email protected]] 

Question:

Why is the Contact.ID == 0 in the second and third contacts rather being 1 and 2 respectively, despite the parameterized constructor being always called and always increment the ID property?


Solution

  • Your default constructor doesn't do what you think it does:

    public Contact()
    {
        new Contact("ANONYMOUS", null, "[email protected]");
    }
    

    This will construct a new Contact and then discard it, the current instance will get all default values. Here is the syntax you're after:

    public Contact()
      : this("ANONYMOUS", null, "[email protected]")
    {
    }