我试图从RelayCommand示例中了解这一行中参数参数的含义和用法:
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"; } } }I am trying to understand the meaning and use of the param parameter in this line taken from a RelayCommand example:
return new RelayCommand(param => MessageBox.Show("It worked."));First, I understand that the "param" parameter has nothing to do with the "params" keyword, is this correct?
public int Add(params int[] list) { int sum = 0; foreach (int i in list) sum += i; return sum; }Second, what kind of delegate code do I have to add to get the following example to work?
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问题中的任何一个:
你能解释lambda表达式吗? 什么是拉姆达 C#lambda表达式,为什么我应该使用这个param isn't a keyword. It's the parameter for a lambda expression in your sample. You'd need to make your method take a delegate or an expression tree, e.g.
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"; } } }The Func<int,int> could actually be any Func<T,int> (or Func<T,long> etc) because your lambda expression doesn't use param anywhere. Alternatively it could be an Expression<Func<int,int>> etc.
I suggest you read up on lambda expressions for more details, for instance in any of these SO questions:
Can you explain lambda expressions What is a lambda C# lambda expression, why should I use this更多推荐
param,return,RelayCommand,params,电脑培训,计算机培训,IT培训"/> <meta name=&q
发布评论