小编son*_*nny的帖子

在另一个类中添加类

我有一个这样的课:

[System.Serializable]
public class arrayData
{
    public List<arrayData2> Levels;
}
public List<arrayData> Zones;

[System.Serializable]
public class arrayData2 { }
Run Code Online (Sandbox Code Playgroud)

我应该在Levels集合中添加一个项目:

void Start()
{
    Zones.Add(new arrayData());
    Zones[0].Levels.Add(new arrayData2());
}
Run Code Online (Sandbox Code Playgroud)

该方法在运行时不断给出此错误:

NullReferenceException:未将对象引用设置为对象的实例

有人知道我错过了什么吗?

c# generics class list

0
推荐指数
1
解决办法
130
查看次数

向Func添加方法

我有一个Func列表,我想添加元素.如果我在Start上添加它们,如下所示,没问题:

        public List<System.Func<bool>> conditions = new List<System.Func<bool>>();

        void Start()
        {
        conditions.Add(Iamdead);
        conditions.Add(Iamalive);
        }

        bool Iamdead()
        {
        ...
        return ...;
        }
        bool Iamalive()
        {
        ...
        return ...;
        }
Run Code Online (Sandbox Code Playgroud)

但我想在没有Start的情况下定义列表,以便我有一个干净的方法列表,我可以将其视为连续的元素.我尝试过经典格式:

public List<System.Func<bool>> conditions = new List<System.Func<bool>>()
{
        bool Iamdead()
        {
        ...
        return ...;
        }
        ,
        bool Iamalive()
        {
        ...
        return ...;
        }
};
Run Code Online (Sandbox Code Playgroud)

这给了我解析错误

我试过这样的:

public List<System.Func<bool>> conditions = new List<System.Func<bool>>()
    {
Iamdead,Iamalive
    };

        static bool Iamdead()
        {
        ...
        return ...;
        }
        static bool Iamalive()
        {
        ...
        return ...;
        }
Run Code Online (Sandbox Code Playgroud)

这只有在方法是静态的情况下才有效,但我不希望它们是静态的.没有静态,它不起作用.我似乎无法理解这里的数据结构.谁能告诉我在列表中定义Func的正确方法?

谢谢

c# list func

0
推荐指数
1
解决办法
718
查看次数

标签 统计

c# ×2

list ×2

class ×1

func ×1

generics ×1