Error: Cannot convert from method group to string

561 views Asked by At

I'm following a tutorial and getting an error:

"cannot convert from 'method group' to 'string'".

I'm using the .net 5.0 framework in the cs project.

Below is the code. The error is coming on the line:

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

There are 1 answers

0
Vincent Bitter On BEST ANSWER

I think the compiler might be confusing your First-property with the First() method of Linq. Are you using .NET 5 or a preview version of .NET 6?

You can try changing the names of First and Last to FirstName and LastName to see if it has to do with this, or remove the System.Linq using on top of your file.