English 中文(简体)
参考方法C#
原标题:References to methods C#
  • 时间:2011-06-01 02:13:31
  •  标签:
  • c#
  • python

I m just wondering if there is a C# equivilent for this python code. I want to store the names of methods in some sort of collection and call them later on. I have searched, but I really don t know what to look for.

例如,在python中,我可以做到:

def add_one(x):
  return x + 1
def double_it(x):
  return x*2

maths_rules = [add_one, double_it]
def do_maths(maths_rules, x):
  for func in maths_rules:
    x = func(x)
  return x

print do_maths(maths_rules, 9)
# >>> 20

这是一个愚蠢的例子,但你应该明白。

最佳回答

Yes, you can use delegates. For this one use Func<int, int>. like:

int addone(int x)
{
    return x + 1;
}

并且大体上:

Func<int, int> myFunc = new Func<int, int>(addone);
myFunc(5); // to use it, you can pass it as you like

代码示例:

static int add_one(int x)
{
    return x + 1;
}
static int double_it(int x)
{
    return x * 2;
}
static int do_maths(List<Func<int, int>> math_rules, int x)
{
    foreach(var func in math_rules)
        x = func(x);
    return x;
}
static void Main(string[] Args)
{
    List<Func<int, int>> math_rules = new List<Func<int, int>>();
    math_rules.Add(new Func<int, int>(add_one));
    math_rules.Add(new Func<int, int>(double_it));
    Console.WriteLine(do_maths(math_rules, 9)); // 20
}

或者按照评论中的建议使用lambdas:

static int do_maths(List<Func<int, int>> math_rules, int x)
{
    foreach(var func in math_rules)
        x = func(x);
    return x;
}
static void Main(string[] Args)
{
    List<Func<int, int>> math_rules = new List<Func<int, int>>();
    math_rules.Add(new Func<int, int>((x) => (x + 1)));
    math_rules.Add(new Func<int, int>((x) => (x * 2)));
    Console.WriteLine(do_maths(math_rules, 9)); // 20
}
问题回答

您正在查找代表

A delegate is a type that defines a method signature. When you instantiate a delegate, you can associate its instance with any method with a compatible signature. You can invoke (or call) the method through the delegate instance.

您在C#中的示例,使用函数<;T,TResult>;委托

int add_one(int x) { return x + 1; }
int double_it(int x) { return x * 2; }

var maths_rules = new List<Func<int,int>> { add_one, double_it };

int do_maths(IEnumerable<Func<int,int>> maths_rules, int x)
{
    foreach (var func in maths_rules)
    {
        x = func(x);
    }
    return x;
}

Console.WriteLine(do_maths(maths_rules, 9));
// prints "20"




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签