Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我能';是否将泛型类型与lamba表达式一起使用?_C#_Generics_Lambda_Generic List - Fatal编程技术网

C# 为什么我能';是否将泛型类型与lamba表达式一起使用?

C# 为什么我能';是否将泛型类型与lamba表达式一起使用?,c#,generics,lambda,generic-list,C#,Generics,Lambda,Generic List,为什么我不能在方法中使用以下lambda?在什么情况下允许使用泛型类型 例如,我知道我可以在类中使用泛型: //Valid public class GenericList<T> { void Add(T input) { } } //有效 公共类泛型列表 { void Add(T输入){} } 但在以下代码中,我不能: //Not valid private void Timer1_Tick(object sender, EventArgs e) { /

为什么我不能在方法中使用以下lambda?在什么情况下允许使用泛型类型

例如,我知道我可以在类中使用泛型:

//Valid
public class GenericList<T>
{
    void Add(T input) { }
}
//有效
公共类泛型列表
{
void Add(T输入){}
}
但在以下代码中,我不能:

 //Not valid
 private void Timer1_Tick(object sender, EventArgs e)
 {
     //Edit:
     //As you can guess this is a timer event and Action is called
     //only in this scope but possibly more than one time
     //that's why i though to make it Action method. 

     //In the following line `Τ` is not recognized...
     Action<List<T>, List<T>> syncLists = (l1, l2) => { ... };
 }
//无效
私有无效计时器1_刻度(对象发送方,事件参数e)
{
//编辑:
//正如您可以猜到的,这是一个计时器事件,并且调用了Action
//仅在此范围内,但可能不止一次
//这就是为什么我想让它成为一种行动方法。
//在下一行中,'t'未被识别。。。
动作同步列表=(l1,l2)=>{…};
}

**我不认为它是特定于框架的,但为了防止我使用3.5,您需要在方法签名中公开TypeParameter,如下所示:

private void Timer1_Tick<T>(object sender, EventArgs e)
 {
     Action<List<T>, List<T>> syncLists = (l1, l2) => { ... };
 }
private void Timer1\u勾选(对象发送方,事件参数e)
{
动作同步列表=(l1,l2)=>{…};
}
否则,客户机无法指定该方法的行为方式,以及操作的类型

在某些情况下更合适的另一种技术是在类签名中公开类型参数,因此:

public class TestClass<T> {
    private void Timer1_Tick(object sender, EventArgs e)
     {
         Action<List<T>, List<T>> syncLists = (l1, l2) => { ... };
     }
}
公共类TestClass{
私有无效计时器1_刻度(对象发送方,事件参数e)
{
动作同步列表=(l1,l2)=>{…};
}
}

您需要在方法签名中公开TypeParameter,如下所示:

private void Timer1_Tick<T>(object sender, EventArgs e)
 {
     Action<List<T>, List<T>> syncLists = (l1, l2) => { ... };
 }
private void Timer1\u勾选(对象发送方,事件参数e)
{
动作同步列表=(l1,l2)=>{…};
}
否则,客户机无法指定该方法的行为方式,以及操作的类型

在某些情况下更合适的另一种技术是在类签名中公开类型参数,因此:

public class TestClass<T> {
    private void Timer1_Tick(object sender, EventArgs e)
     {
         Action<List<T>, List<T>> syncLists = (l1, l2) => { ... };
     }
}
公共类TestClass{
私有无效计时器1_刻度(对象发送方,事件参数e)
{
动作同步列表=(l1,l2)=>{…};
}
}

您需要在方法签名中公开TypeParameter,如下所示:

private void Timer1_Tick<T>(object sender, EventArgs e)
 {
     Action<List<T>, List<T>> syncLists = (l1, l2) => { ... };
 }
private void Timer1\u勾选(对象发送方,事件参数e)
{
动作同步列表=(l1,l2)=>{…};
}
否则,客户机无法指定该方法的行为方式,以及操作的类型

在某些情况下更合适的另一种技术是在类签名中公开类型参数,因此:

public class TestClass<T> {
    private void Timer1_Tick(object sender, EventArgs e)
     {
         Action<List<T>, List<T>> syncLists = (l1, l2) => { ... };
     }
}
公共类TestClass{
私有无效计时器1_刻度(对象发送方,事件参数e)
{
动作同步列表=(l1,l2)=>{…};
}
}

您需要在方法签名中公开TypeParameter,如下所示:

private void Timer1_Tick<T>(object sender, EventArgs e)
 {
     Action<List<T>, List<T>> syncLists = (l1, l2) => { ... };
 }
private void Timer1\u勾选(对象发送方,事件参数e)
{
动作同步列表=(l1,l2)=>{…};
}
否则,客户机无法指定该方法的行为方式,以及操作的类型

在某些情况下更合适的另一种技术是在类签名中公开类型参数,因此:

public class TestClass<T> {
    private void Timer1_Tick(object sender, EventArgs e)
     {
         Action<List<T>, List<T>> syncLists = (l1, l2) => { ... };
     }
}
公共类TestClass{
私有无效计时器1_刻度(对象发送方,事件参数e)
{
动作同步列表=(l1,l2)=>{…};
}
}


是否在外部类定义中定义了
T
?您想实现什么?在第二个代码示例中,
T
应该表示什么类型?对于泛型,最终必须指定类型。在您的代码中,
T
的类型在哪里指定?我只需要在
Timer
范围内定义一个小函数
T
可以是任何类,因为我需要泛型类型。可能我可以使用
dynamic
,但这并不可取。是否在外部类定义中定义了
T
?您将尝试实现什么?在第二个代码示例中,
T
应该表示什么类型?对于泛型,最终必须指定类型。在您的代码中,
T
的类型在哪里指定?我只需要在
Timer
范围内定义一个小函数
T
可以是任何类,因为我需要泛型类型。可能我可以使用
dynamic
,但这并不可取。是否在外部类定义中定义了
T
?您将尝试实现什么?在第二个代码示例中,
T
应该表示什么类型?对于泛型,最终必须指定类型。在您的代码中,
T
的类型在哪里指定?我只需要在
Timer
范围内定义一个小函数
T
可以是任何类,因为我需要泛型类型。可能我可以使用
dynamic
,但这并不可取。是否在外部类定义中定义了
T
?您将尝试实现什么?在第二个代码示例中,
T
应该表示什么类型?对于泛型,最终必须指定类型。在您的代码中,
T
的类型在哪里指定?我只需要在
Timer
范围内定义一个小函数
T
可以是任何类,因为我需要泛型类型。可能我可以使用
dynamic
,但不可取。该方法看起来像事件处理程序,所以我认为OP不能使其通用。@2kay:这似乎不寻常,但很难在如此广泛的领域中对业务需求做出准确的一般性陈述。这样做我能实现什么?我的想法是在计时器内使用局部作用域函数以避免重复。问题是我想要一个泛型类型列表,我不喜欢使用动态。谢谢你的帮助response@JorgeCode当前位置您的业务需求仍然很模糊。任何更具体的帮助都需要您提供一个更清晰的业务案例,可能有一个或两个特定的预期用途示例。方法看起来像事件处理程序,所以我认为OP不能使其通用。@2kay:it se