我怎样才能得到这个例子使用C#中的“参数”工作?(How can I get this example using “param” in C# to work?)

编程入门 行业动态 更新时间:2024-10-26 23:42:10
我怎样才能得到这个例子使用C#中的“参数”工作?(How can I get this example using “param” in C# to work?)

我试图从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

本文发布于:2023-07-24 08:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1243465.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:例子   参数   工作   work   param

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!