今天,按照一本书的教程(字面意思),我遇到了一个意想不到的错误:
operator '!=' cannot be applied to operands of type 'char' and 'string'
在这部分代码中:
public void displayFullName()
{
StringBuilder FullName = new StringBuilder();
FullName.Append(firstName);
FullName.Append(" ");
if (middleName[0] != "") //ERROR occurs here
{
FullName.Append(middleName[0]);
FullName.Append(". ");
}
FullName.Append(lastName);
Console.WriteLine(FullName);
}
当然 middleName 被声明为:
class Person
{
private string firstName;
private string middleName; //obiously declared as string
private string lastName;
private int age;
(...the rest of code...)
那么,问题是您将如何解决此错误?