在变量中引用函数? [英] Referencing a function in a variable?
本文介绍了在变量中引用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个功能.我希望在变量中添加对此功能的引用.
Say I have a function. I wish to add a reference to this function in a variable.
因此,我可以从变量"bar"中调用函数"foo(bool foobar)",就好像它是一个函数一样.例如. 'bar(foobar)'.
So I could call the function 'foo(bool foobar)' from a variable 'bar', as if it was a function. EG. 'bar(foobar)'.
如何?
推荐答案
听起来您想将Func
保存到变量中以备后用.在此处:
It sounds like you want to save a Func
to a variable for later use. Take a look at the examples here:
using System;
public class GenericFunc
{
public static void Main()
{
// Instantiate delegate to reference UppercaseString method
Func<string, string> convertMethod = UppercaseString;
string name = "Dakota";
// Use delegate instance to call UppercaseString method
Console.WriteLine(convertMethod(name));
}
private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
}
查看方法UppercaseString
如何保存到名为convertMethod
的变量,然后可以在以后调用该变量:convertMethod(name)
.
See how the method UppercaseString
is saved to a variable called convertMethod
which can then later be called: convertMethod(name)
.
这篇关于在变量中引用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文