-1

我不明白为什么会发生以下情况。请参阅代码和输出示例。为什么当 3 个数组元素被明确设置为不同的值时,它们会输出相同的值。

吉姆
·弗兰克
·詹姆斯

詹姆斯
詹姆斯
詹姆斯

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;
    }
    }
}
4

2 回答 2

0
 usersUser userConfig = new usersUser();

你只声明一次。然后,您只需每次都用另一个值覆盖该userConfig.username字段。试试这样:

usersUser userConfig = new usersUser(); // <-- new instance
userConfig.username = "Jim";
allUsers.user[0] = userConfig;


usersUser userConfig1 = new usersUser(); // <-- another new instance
userConfig1.username = "Jim";
allUsers.user[0] = userConfig1;
于 2012-09-29T18:02:37.413 回答
0

因为您在分配之前一遍又一遍地更改同一个对象,因此即使对于已经分配的对象,您也会对其进行更改,因此您将其设置为的姓氏就是坚持的那个。你必须这样做:

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();
    }
}

为数组中的每个索引创建新对象,而不是使用相同的三次。

于 2012-09-29T18:04:28.207 回答