views:

53

answers:

1

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 parametertized 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 = {

         new Contact(),

         new Contact {

            Name = "contactName1",

            Age = 40,

            Email = "[email protected]"

         },

         new Contact {

            Name = "contactName2",

            Age = 41,

            Email = "[email protected]"

         },

         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 ??

*/

+6  A: 

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]")
{
}
Dean Harding
that was embarrassingly quick :)
mumtaz
Actually what frustrated me was the fact that when the default constructor called the parameterized constructor the ID field would increment while I was in there, but when the execution flow would return to the call site everything would vanish as if nothing had happened.
mumtaz