如何一次性给对象数组赋值?

0xiaoxin 2013-09-03 07:00:26
一个SE类里面是
public class SE
{
public int Age { get; set; }
public string Name { get; set; }
public int ID { get; set; }
private string assess = "未评价";

public string Assess
{
get { return assess; }
set { assess = value; }
}
private int score = 0;

public int Score
{
get { return score; }
set { score = value; }
}
}
然后我创建一个SE类的数组
SE[] se=new SE[3];
public FrmShow()
{
InitializeComponent();
}
public void BindLv()
{
se[0] = new SE();
原本赋值是se[0].name="XXX";
se[0].id=000;
请问我可以一次性的赋值吗?
new的时候直接赋值?
}
...全文
479 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
灬浪子灬 2013-09-03
  • 打赏
  • 举报
回复
引用 6 楼 sp1234 的回复:
c#语言可以写得很优雅:
SE[] se = { new SE { Name = "a" }, new SE { Name = "b" }, new SE { Name = "c" } };
  • 打赏
  • 举报
回复
c#语言可以写得很优雅:
SE[] se = { new SE { Name = "a" }, new SE { Name = "b" }, new SE { Name = "c" } };
Andy__Huang 2013-09-03
  • 打赏
  • 举报
回复
给属性设置默认值,
全栈极简 2013-09-03
  • 打赏
  • 举报
回复
上接2楼的代码,如果初始化了一个SE数组对象,那么SE[0]、SE[1]、SE[2]就分别为3个对象。刚测试过,是可以取出SE[0].Age属性的,并且为10的。
0xiaoxin 2013-09-03
  • 打赏
  • 举报
回复
引用 2 楼 guwei4037 的回复:
在SE类中加个构造函数:
public SE(int age, string name, int id, int score)
        {
            this.Age = age;
            this.Name = name;
            this.ID = id;
          
然后就可以直接初始化各个变量。
SE[] se = new SE[] { 
                new SE(10,"a",1,99){},
                new SE(11,"b",1,98){},
                new SE(12,"c",1,97){}
            };
请问我如何访问数组中的不同元素呢? 对象名[下标]不可以呀.
全栈极简 2013-09-03
  • 打赏
  • 举报
回复
在SE类中加个构造函数:
public SE(int age, string name, int id, int score)
        {
            this.Age = age;
            this.Name = name;
            this.ID = id;
          
然后就可以直接初始化各个变量。
SE[] se = new SE[] { 
                new SE(10,"a",1,99){},
                new SE(11,"b",1,98){},
                new SE(12,"c",1,97){}
            };
threenewbee 2013-09-03
  • 打赏
  • 举报
回复
SE[] se = new SE[] { new SE() { Name = "a" }, new SE() { Name = "b" }, new SE() { Name = "c" } };

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧