Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法从';转换;。。。通用列表<;Game02.人类>';至';Game02.人类';_C# - Fatal编程技术网

C# 无法从';转换;。。。通用列表<;Game02.人类>';至';Game02.人类';

C# 无法从';转换;。。。通用列表<;Game02.人类>';至';Game02.人类';,c#,C#,我正试图通过将NPC添加到房间的humansHere列表中来实现将NPC“发送”到特定房间的功能,以及获取此列表的功能(稍后打印,但我不需要帮助)。但我得到了这个错误信息: 参数1:无法从“System.Collections.Generic.List”转换为“Game02.Human” 一旦我解决了这个问题,我相信我会解决剩下的问题,所以请忽略这个问题:我需要知道如何为特定的房间调用这个函数。比如: LivingRoom.GetHumansHere()或Kitchen.SetHumansHer

我正试图通过将NPC添加到房间的humansHere列表中来实现将NPC“发送”到特定房间的功能,以及获取此列表的功能(稍后打印,但我不需要帮助)。但我得到了这个错误信息:

参数1:无法从“System.Collections.Generic.List”转换为“Game02.Human”

一旦我解决了这个问题,我相信我会解决剩下的问题,所以请忽略这个问题:我需要知道如何为特定的房间调用这个函数。比如:
LivingRoom.GetHumansHere()
Kitchen.SetHumansHere(_lyndonJohnson)
。或者这会像现在这样工作吗

public class Room
{
    public int ID { get; set; }
    [...]

    private List<Human> humansHere;

    public List<Human> GetHumansHere()
    {
        return humansHere;
    }

    public void SetHumansHere(List<Human> x)
    {
        humansHere.Add(x);
    }
}

public class Human : LivingCreature
{
    public int Gold { get; set; }
    public List<InventoryItem> Inventory { get; set; }

    public Human(string name, int currentHitPoints, int maximumHitPoints, int gold) : base(name, currentHitPoints, maximumHitPoints)
    {
        Gold = gold;
    }
}
公共教室
{
公共int ID{get;set;}
[...]
私人名单;
公共列表GetHumansHere()
{
回归人性;
}
公共无效设置HumanShere(列表x)
{
添加(x);
}
}
公共类人类:活的生物
{
公共整数Gold{get;set;}
公共列表清单{get;set;}
公共人员(字符串名称,int currentHitPoints,int maximumHitPoints,int gold):基本(名称,currentHitPoints,maximumHitPoints)
{
黄金=黄金;
}
}
感谢Dmitry的帮助,感谢Jonathan对问题的解释:


问题是您试图将一组人员添加到列表中,而不是将一个人添加到列表中


您需要使用List.AddRange,将指定集合的元素添加到列表的末尾

public void SetHumansHere(List<Human> x)
{
    humansHere.AddRange(x);
}
public void SetHumansHere(列表x)
{
人形范围(x);
}

您需要使用列表。AddRange将指定集合的元素添加到列表的末尾

public void SetHumansHere(List<Human> x)
{
    humansHere.AddRange(x);
}
public void SetHumansHere(列表x)
{
人形范围(x);
}
有两种可能性:

如果只想添加一个
人员
,请更改方法的签名:

public void SetHumansHere(Human person)
{
    if (null == person)
        throw new ArgumentNullException(nameof(person)); 

    humansHere.Add(person);
}
如果要一次性添加
人员的集合
,请使用
AddRange

// IEnumerable<Human> - let's generalize the method
// and allow to add not only List, but other collections, say, array 
public void SetHumansHere(IEnumerable<Human> persons)
{
    if (null == persons)
        throw new ArgumentNullException(nameof(persons)); 

    humansHere.AddRange(persons);
}
//IEnumerable-让我们概括一下该方法
//并且不仅允许添加列表,还允许添加其他集合,比如数组
公共无效设置人员(IEnumbers)
{
if(null==人)
抛出新的异常(姓名(人));
humansHere.AddRange(人);
}
有两种可能性:

如果只想添加一个
人员
,请更改方法的签名:

public void SetHumansHere(Human person)
{
    if (null == person)
        throw new ArgumentNullException(nameof(person)); 

    humansHere.Add(person);
}
如果要一次性添加
人员的集合
,请使用
AddRange

// IEnumerable<Human> - let's generalize the method
// and allow to add not only List, but other collections, say, array 
public void SetHumansHere(IEnumerable<Human> persons)
{
    if (null == persons)
        throw new ArgumentNullException(nameof(persons)); 

    humansHere.AddRange(persons);
}
//IEnumerable-让我们概括一下该方法
//并且不仅允许添加列表,还允许添加其他集合,比如数组
公共无效设置人员(IEnumbers)
{
if(null==人)
抛出新的异常(姓名(人));
humansHere.AddRange(人);
}

humansHere.AddRange(x)
x
中的所有值添加到
humansHere
;或者将方法的签名更改为
public void SetHumansHere(Human x)
问题是您试图将一组人员添加到列表中,而不是将一个人添加到列表
humansHere.AddRange(x)
x
中的所有值添加到
humansHere
;或者将方法的签名更改为
public void SetHumansHere(Human x)
问题是您试图将一组人员添加到列表中,而不是将一个人添加到列表中