【问题标题】:How to store method inside of method in a LIST?How to store method inside of method in a LIST?
【发布时间】:2022-12-02 01:04:13
【问题描述】:

How do I store method inside a method in a List<>?

I know its a bit confusing but idk how to explain it in more details. That's how I understand my problem.

So I wanna store methods inside a List<> and one of the method consists another method inside it. Since the List is executed as first in first out, when it comes to the method with another method inside, I want it to execute the child until it finishes before it continues to another element in the parent List.

For scenario, the method with another method inside is A Loop. I want it to iterate until certain condition is met then it stops. Then, continues to another method store in parents List.

As for now, I can store method inside List. It just idk how to execute the method inside method.

【问题讨论】:

  • hi and elcome. Please post the code of "the method consists another method inside it"
  • I don't understand your question or problem, and by the sounds of it you're probably approaching your actual problem, incorrectly. But if you still want to store methods in a list you could use List of Func<T,TResult>.

标签: c# unityscript


【解决方案1】:

You can store delegates within a list.

List<Func<int, int>> funcs = new List<Func<int, int>>();

funcs.Add(i => i * 2);

funcs[0].Invoke(1); //returns 2

Please, take a look at Func, Action and Predicate delegates, see Delegates: Predicate vs. Action vs. Func .

【讨论】:

    猜你喜欢
    • 2016-02-01
    • 2022-12-26
    • 2022-11-20
    • 2022-12-01
    • 2022-12-27
    • 2022-12-26
    • 2022-12-26
    • 2022-11-09
    • 2022-12-19
    相关资源
    最近更新 更多