簡體   English   中英

c# - 如何在不調用它的情況下將 function 作為參數(以參數作為值)傳遞

[英]c# - how to pass function as parameter (with parameters as values) without invoking it

我想將一個 function 作為參數傳遞給另一個 function(帶有實際值),然后執行它。 這是 JavaScript 代碼:

function functionToPass1 (x,y) {
    console.log(x, y);
}

function functionToPass2 (x,y,z) {
    console.log(x, y, z);
}

function mainFunction(param1, param2, functionToPass){
  console.log(param1, param2);
  functionToPass();

}

mainFunction("a", "b", function(){
    functionToPass2(1, 2, 3) ;
});

如何在 C#(或 VB.Net)中寫這個?

Func<> 委托是您正在尋找以另一種方法傳遞方法的方法。 下面我寫了一個小例子,大家可以根據自己的需要來應用。 Func<> 是一個通用委托。 它可以是 Func<int,string> Func<int,int,bool> 等等。 最后一個代表返回類型,其他代表方法的輸入參數類型。

        static void Main(string[] args)
        {
            MethodB(MethodA);
            Console.ReadLine();
        }

        static string MethodA(string message) //Func<string,string>
        {
            return message;
        }

        static void MethodB(Func<string, string> method)
        {  
            Console.WriteLine(method("I am MethodA"));
        }

有關更多詳細信息,您可以查看此鏈接 => https://www.tutorialsteacher.com/csharp/csharp-func-delegate

謝謝大家的貢獻,你們的幫助對我來說意味着一切。 我唯一需要弄清楚的是要傳遞的匿名 function 。 C# 等效如下:

string functionToPass1(int i, string x, string y)
{
  return string.Join(',', new[] { i.ToString(), x, y });
}

string functionToPass2(int i, string x, string y, string z)
{
  return string.Join(',', new[] { i.ToString(), x, y, z });
}

string mainFunction(string param1, string param2, Func<int,string>? functionToPass)
{
  Console.WriteLine(param1);
  Console.WriteLine(param2);
  var res = "";
  int i = 5;
  if (functionToPass != null)
    res = functionToPass(i);
  return res;
}

var res = "";

res = mainFunction("a", "b", (x) =>
{
  return functionToPass2(x, "1", "2", "3");
});

Console.WriteLine(res);
Console.WriteLine("---------------");

res = mainFunction("a", "b", (x) =>
{
  return functionToPass1(x, "1", "2");
});

Console.WriteLine(res);
Console.WriteLine("---------------");

res = mainFunction("a", "b", null);

Console.WriteLine(res);
Console.WriteLine("---------------");
Console.ReadLine();

我沒有刪除第一條評論,以防將來對其他人有用。 我對您刷新的代碼進行了一些更改。 我試圖盡可能地創建一個通用結構。 但是,Javascript 比 C# 靈活得多。 換句話說,即使結構是通用的,在某些時候我們也必須打破這種通用性。

        static void Main(string[] args)
        {
            MainFunction<string, string, int>("a", "b", FunctionToPass2<int, int, int>);
            Console.ReadLine();
        }

        static void FunctionToPass1<T1, T2>(T1 x, T2 y) 
        {
            Console.WriteLine(x + " " + y);
        }

        static void FunctionToPass2<T1, T2, T3>(T1 x, T2 y, T3 z)
        {
            Console.WriteLine(x + " " + y + " " + z);
        }

        static void MainFunction<T1, T2, T3>(T1 param1, T2 param2, Action<T3, T3, T3> functionToPass)
        {
            Console.WriteLine(param1 + " " + param2 + "\n");
            FunctionToPass2(1, 2, 3);
        }

如果我們的參數類型是確定的,我相信我們會通過不寫泛型來提出一個更簡單的解決方案。 例如,我假設 Action<T3, T3, T3> 委托的 T3 對於此解決方案是 int,因為您的 arguments 是 integer。 在這里,我們必須稍微打破“通用”結構。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM