gpt4 book ai didi

c# - 将类型字符串转换为类型对象

转载 作者:行者123 更新时间:2023-11-30 13:24:33 26 4
gpt4 key购买 nike

我是 C# 新手。我需要在控制台应用程序中显示用户全名,但我一直收到此错误

Cannot implicitly convert type 'string' to 'UserCustomerNotes.User'

当我只使用 return user 时,我的代码有效,但它在应用程序中显示的所有内容都是“UserCustomerNotes.User”,我需要显示用户的全名。谁能帮忙。这是我的代码:

public static User FindUser(User[] users, int noteid)
{
foreach (User user in users)
if (user.ID == noteid) return user.FullName;
return null;
}

最佳答案

您的方法需要 typeUser 的返回值,如果您希望它返回名称 - 将其更改为:

public static **string** FindUser(User[] users, int noteid)

或者,或者:

if (user.ID == noteid) return user;

关于c# - 将类型字符串转换为类型对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3574987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com