我有一小段代码:
public DisabledStudent(int id, int age, bool requiressupport)
{
this.requiressupport = requiressupport;
}
该数组类似于以下内容:
public partial class Form1 : Form
{
const int MaxStudents = 4;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Student[] studentList;
studentList = new Student[4];
studentList[0] = new Student(51584, 17);
studentList[1] = new Student(51585, 19);
studentList[2] = new Student(51586, 15);
studentList[3] = new Student(51587, 20);
for (int i = 0; i < MaxStudents; i++)
{
lstStudents.Items.AddRange(studentList);
}
}
我想要做的是从学生数组输出一个字符串,基本上显示文本的不同部分,具体取决于requiressupport
布尔值是true
还是false
:
public override string ToString()
{
return string.Format("Disabled Student - ID: {0} (Age {1})", this.Number, this.Age);
}
如果Disabled Student - ID: 45132 (Age 19) with support
布尔为requiressupport
,我希望上面的语句基本上说true
如果Disabled Student - ID: 45132 (Age 19) without support
布尔是requiressupport
,则false
,但我不确定我是怎么做到的?
答案 0 :(得分:2)
您可以选择使用?: conditional operator
public override string ToString()
{
return string.Format("Disabled Student - ID: {0} (Age {1}) {2}", this.Number, this.Age, this.requiressupport ? "with support" : "without support");
}