我不明白为什么会发生以下情况。请参阅代码和输出示例。为什么当 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;
}
}
}