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