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 8677931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:35:21+00:00 2026-06-12T20:35:21+00:00

I cant understand why the below is happening. See the code and output example.

  • 0

I cant understand why the below is happening.
See the code and output example.
Why are the 3 array elements outputting the same value when they are clearly being set to different values.

Jim
Frank
James

James
James
James

class Program
{
    static void Main(string[] args)
    {

        users allUsers = new users();
        allUsers.user = new usersUser[3];
        usersUser userConfig = new usersUser();

        userConfig.username = "Jim";
        allUsers.user[0] = userConfig;
        Console.WriteLine(allUsers.user[0].username);

        userConfig.username = "Frank";
        allUsers.user[1] = userConfig;
        Console.WriteLine(allUsers.user[1].username);

        userConfig.username = "James";
        allUsers.user[2] = userConfig;
        Console.WriteLine(allUsers.user[2].username);

        Console.WriteLine("");

        Console.WriteLine(allUsers.user[0].username);
        Console.WriteLine(allUsers.user[1].username);
        Console.WriteLine(allUsers.user[2].username);

        Console.ReadLine();
    }
}


using System.Xml.Serialization;


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class users {

    private usersUser[] userField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("user")]
    public usersUser[] user {
    get {
        return this.userField;
    }
    set {
        this.userField = value;
    }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class usersUser {

    private string usernameField;

    /// <remarks/>
    public string username {
    get {
        return this.usernameField;
    }
    set {
        this.usernameField = value;
    }
    }
}
  • 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-06-12T20:35:22+00:00Added an answer on June 12, 2026 at 8:35 pm

    Because you are changing the same object over and over before assigning it, hence you are changing it even for the ones that are already assigned, the last name you set it to is the one that sticks. You have to do it like:

    class Program
    {
        static void Main(string[] args)
        {
    
            users allUsers = new users();
            allUsers.user = new usersUser[3];
            usersUser userConfig = new usersUser();
    
            userConfig.username = "Jim";
            allUsers.user[0] = userConfig;
            Console.WriteLine(allUsers.user[0].username);
    
            userConfig = new usersUser();
            userConfig.username = "Frank";
            allUsers.user[1] = userConfig;
            Console.WriteLine(allUsers.user[1].username);
    
            userConfig = new usersUser();
            userConfig.username = "James";
            allUsers.user[2] = userConfig;
            Console.WriteLine(allUsers.user[2].username);
    
            Console.WriteLine("");
    
            Console.WriteLine(allUsers.user[0].username);
            Console.WriteLine(allUsers.user[1].username);
            Console.WriteLine(allUsers.user[2].username);
    
            Console.ReadLine();
        }
    }
    

    Create new objects for each index in the array, instead of using the same three times.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't understand what the below ruby code does. Can anyone give me some
I really can't understand how the below piece of code is working... options.each {
I can't understand how works the code below: class Host: name = None fileList
I found the code from the net in which i cant understand this line:-
I dont really understand whats going on and cant see the difference: I'm downloaded
The following method shows NullPointerException. I can't understand what is wrong with below code.
Could anyone help me understand why the below code doesn't compile (VS2010) when the
I'm studing C++ and I can't understand the meaning of the boldface sentence below:
I cant understand what this warning I get on Xcode is about. Searching for
I've installed UML plugin but I cant understand how create UML automatically

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.