无法从 'string' 转换为 'object'

cannot convert from 'string' to 'object'

我正在做作业,我无法完成需要查看队列中是否包含姓名的部分:

public struct person
{
    public string name;
    public string bday;
    public int age;
}

class Program
{
    static void Main(string[] args)
    {
        person personaStruct = new person();
        Queue<person> personQueue = new Queue<person>();

        Console.WriteLine("Type a name");
        personStruct.name = Console.ReadLine();

        if (personQueue.Contains(personStruct.name))
        {
            Console.WriteLine(personStruct.name);
            Console.WriteLine(personStruct.bday);
            Console.WriteLine(personStruct.age);
        }
        else
        {
            Console.WriteLine("Doesn't exist!");
        }
    }
}

我希望它能显示完整的队列(姓名、生日、年龄)

要按姓名查找匹配的人,请按姓名过滤您的队列,然后查找所有剩余的匹配项。假设您的队列中只能有一个匹配项,请获取第一个匹配项并将其打印给用户。如果 person 是 class 而不是结构,您也可以只使用 FirstOrDefault 并检查 null 但是对于结构,这可能是最简单的方法。

var matchingPeopele = personQueue.Where(p => p.name == personStruct.name);
if (matchingPeopele.Any())
{
    var match = matchingPeopele.First();
    Console.WriteLine(match.name);
    Console.WriteLine(match.bday);
    Console.WriteLine(match.age);
}
else
{
    Console.WriteLine("Doesn't exist!");
}

关于你的评论,你的老师还没有讲过 LINQ,这是另一个版本。在这一点上,我基本上是在为您做功课,但请在尝试代码时尽最大努力真正理解发生了什么。

static void Main(string[] args)
{
    person personStruct = new person();
    Queue<person> personQueue = new Queue<person>();

    Console.WriteLine("Type a name");
    personStruct.name = Console.ReadLine();

    var personFound = false;
    foreach(var p in personQueue)
    {
        if (p.name == personStruct.name)
        {
            personFound = true;
            Console.WriteLine(p.name);
            Console.WriteLine(p.bday);
            Console.WriteLine(p.age);
            break;
        }
    }
    if (!personFound)
    {
        Console.WriteLine("Doesn't exist!");
    }
}