![](/img/trans.png)
[英]C# I can't get CreateNoWindow to work - not even the msdn.com example
[英]How can I get this example using “param” in C# to work?
我正在嘗試從RelayCommand示例中了解此行中param參數的含義和用法:
return new RelayCommand(param => MessageBox.Show("It worked."));
首先,我知道“ param”參數與“ params”關鍵字無關 ,這是正確的嗎?
public int Add(params int[] list)
{
int sum = 0;
foreach (int i in list)
sum += i;
return sum;
}
其次,為了使以下示例正常工作, 我必須添加哪種委托代碼 ?
using System;
using System.Collections.Generic;
namespace TestParam222
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("The total is {0}.", Tools.GetTest(param => 23));
Console.ReadLine();
}
}
class Tools
{
public static string GetTest(List<int> integers)
{
return "ok";
}
}
}
param
不是關鍵字。 它是樣本中lambda表達式的參數。 您需要使您的方法采用委托或表達式樹,例如
using System;
using System.Collections.Generic;
namespace TestParam222
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("The total is {0}.", Tools.GetTest(param => 23));
Console.ReadLine();
}
}
class Tools
{
public static string GetTest(Func<int, int> integers)
{
return "ok";
}
}
}
Func<int,int>
實際上可以是任何 Func<T,int>
(或Func<T,long>
等),因為您的lambda表達式在任何地方都不使用param
。 或者,它可以是Expression<Func<int,int>>
等。
我建議您閱讀lambda表達式以了解更多詳細信息,例如,在以下任何SO問題中:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.