我如何使用"得到这个例子参数"在C#中工作? [英] How can I get this example using "param" in C# to work?
问题描述
我想了解的含义和使用参数在此行从RelayCommand例如拍摄参数:
返回新RelayCommand(参数=> MessageBox.Show(它的工作。));
首先,据我所知,参数参数有无关的PARAMS 关键字,这是正确的?
公众诠释添加(PARAMS INT []列表)
{
INT总和= 0;
的foreach(INT我的列表)
总和+ =我;
收益总和;
}
二,我有什么样的委托代码中加入< ?/ strong>即可得到以下示例正常工作
使用系统;
使用System.Collections.Generic;
命名空间TestParam222
{
类节目
{
静态无效的主要(字串[] args)
{
控制台。的WriteLine(总的是{0},Tools.GetTest(参数=> 23));
到Console.ReadLine();
}
}
类工具
{
公共静态字符串GetTest(列表< INT>的整数)
{
返回好;
}
}
}
参数
不是关键字。它是你的样品中lambda表达式的参数。 。你需要让你的方法,采取委托或表达式目录树,例如
使用系统;
使用System.Collections.Generic;
命名空间TestParam222
{
类节目
{
静态无效的主要(字串[] args)
{
控制台。的WriteLine(总的是{0},Tools.GetTest(参数=> 23));
到Console.ReadLine();
}
}
类工具
{
公共静态字符串GetTest(Func键< INT,INT>的整数)
{
返回确定;
}
}
}
的
函数功能:LT; INT,INT>
实际上可能的任何的 Func键< T,INT>
(或 Func键< T,长>
),因为你的lambda表达式没有使用参数
的任何地方。或者它可能是一个表达式来; Func键< INT,INT>>
等
我建议你阅读起来。在任何这些做题lambda表达式的更多详细信息,例如:
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
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:
这篇关于我如何使用"得到这个例子参数"在C#中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!