Error: Cannot convert from method group to string
Error: Cannot convert from method group to string
我正在学习教程并遇到错误:
"cannot convert from 'method group' to 'string'".
我在cs项目中使用的是.net 5.0框架。
下面是代码。错误即将上线:
yield return new Student(_names[index].First, _names[index].Last);
StudentRepository.cs
public record Name(string First, string Last);
public class StudentRepository: IRepository<Student>
{
private Name[] _names = new Name[10];
public StudentRepository()
{
_names[0] = new("Steve", "Smith");
_names[1] = new("Chad", "Smith");
_names[2] = new("Ben", "Smith");
_names[3] = new("Eric", "Smith");
_names[4] = new("Julie", "Lerman");
_names[5] = new("David", "Starr");
_names[6] = new("Aaron", "Skonnard");
_names[7] = new("Aaron", "Stewart");
_names[8] = new("Aaron", "Powell");
_names[9] = new("Aaron", "Frost");
}
public IEnumerable<Student> List()
{
int index = 0;
while (index < _names.Length)
{
yield return new Student(_names[index].First, _names[index].Last);
index++;
}
}
}
Student.cs
public class Student: IComparable<Student>
{
public static int studentCounter = 0;
public string FirstName { get; set; }
public string LastName { get; set; }
public Student(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
studentCounter++;
}
public override string ToString()
{
return $"{FirstName} {LastName}";
}
public int CompareTo(Student other)
{
if (other is null) return 1;
if (other.LastName == this.LastName)
{
return this.FirstName.CompareTo(other.FirstName);
}
return this.LastName.CompareTo(other.LastName);
}
}
我认为编译器可能会将您的 First-属性 与 Linq 的 First() 方法混淆。您使用的是 .NET 5 还是 .NET 6 的预览版?
您可以尝试将 First 和 Last 的名称更改为 FirstName 和 LastName 以查看是否与此有关,或者删除文件顶部的 System.Linq using。
我正在学习教程并遇到错误:
"cannot convert from 'method group' to 'string'".
我在cs项目中使用的是.net 5.0框架。
下面是代码。错误即将上线:
yield return new Student(_names[index].First, _names[index].Last);
StudentRepository.cs
public record Name(string First, string Last);
public class StudentRepository: IRepository<Student>
{
private Name[] _names = new Name[10];
public StudentRepository()
{
_names[0] = new("Steve", "Smith");
_names[1] = new("Chad", "Smith");
_names[2] = new("Ben", "Smith");
_names[3] = new("Eric", "Smith");
_names[4] = new("Julie", "Lerman");
_names[5] = new("David", "Starr");
_names[6] = new("Aaron", "Skonnard");
_names[7] = new("Aaron", "Stewart");
_names[8] = new("Aaron", "Powell");
_names[9] = new("Aaron", "Frost");
}
public IEnumerable<Student> List()
{
int index = 0;
while (index < _names.Length)
{
yield return new Student(_names[index].First, _names[index].Last);
index++;
}
}
}
Student.cs
public class Student: IComparable<Student>
{
public static int studentCounter = 0;
public string FirstName { get; set; }
public string LastName { get; set; }
public Student(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
studentCounter++;
}
public override string ToString()
{
return $"{FirstName} {LastName}";
}
public int CompareTo(Student other)
{
if (other is null) return 1;
if (other.LastName == this.LastName)
{
return this.FirstName.CompareTo(other.FirstName);
}
return this.LastName.CompareTo(other.LastName);
}
}
我认为编译器可能会将您的 First-属性 与 Linq 的 First() 方法混淆。您使用的是 .NET 5 还是 .NET 6 的预览版?
您可以尝试将 First 和 Last 的名称更改为 FirstName 和 LastName 以查看是否与此有关,或者删除文件顶部的 System.Linq using。