Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1097551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:27:04+00:00 2026-05-17T00:27:04+00:00

It took me some debugging to figure this out (or so do I think).

  • 0

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 = "emailaddress1@email.com"
         },

         //Contact 2
         new Contact {
            Name = "contactName2",
            Age = 41,
            Email = "emailaddress2@email.com"
         },

         //Contact 3
         new Contact("contactName3",
            42,
            "emailaddress3@email.com"),
      };

      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, "ANONYMOUS@unknown.com");  
    }

    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=emailaddress1@email.com]  
[Contact: Id=0, Name=contactName2, Age=41, Email=emailaddress2@email.com]  
[Contact: Id=3, Name=contactName3, Age=42, Email=emailaddress3@email.com] 

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:27:05+00:00Added an answer on May 17, 2026 at 12:27 am

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

    public Contact()
    {
        new Contact("ANONYMOUS", null, "ANONYMOUS@unknown.com");
    }
    

    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, "ANONYMOUS@unknown.com")
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I took some code from this Microsoft provided Example which allows me to
I was debugging some C++ code in GDB and I found out that some
I took some sample code to create a drop down menu from this website:
I took some code here: Check if role consists of particular user in DB?
it took me some time to find out that both Eclipse and Aptana get
I took some code from my C# Windows form app which uses the Application
I am trying to build a simple vertical thermometer with CSS. I took some
I took some code from some questions here in SO as well as some
So I took some php code and turned it into a calendar with a
i took some interest in this script http://www.9lessons.info/2009/06/comment-system-with-jquery-ajax-and-php.html and i see that the ajax

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.