2

我有一小段代码:

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

我希望上面的语句基本上说明boolean 是否为Disabled Student - ID: 45132 (Age 19) with support以及boolean是否为,但我不确定我会怎么做?requiressupporttrueDisabled Student - ID: 45132 (Age 19) without supportrequiressupportfalse

4

1 回答 1

2

您的众多选择之一是使用?: 条件运算符

public override string ToString()
{
    return string.Format("Disabled Student - ID: {0} (Age {1}) {2}", this.Number, this.Age, this.requiressupport ? "with support" : "without support");
}
于 2013-10-31T01:56:36.530 回答