不能在泛型 class 中隐式 return 键入 void?
Cannot implicity return type void in generic class?
我正在尝试构建一个简单的泛型 class,它应该将对象和委托作为泛型 parameters.I 当我将方法绑定到事件时收到以下错误:
无法将类型 'void' 隐式转换为 'ConsoleApp3.test.Del'
我不明白为什么我会收到此错误,因为我的委托 returns void 并且附加到事件的方法也 returns void。
这是代码:
interface ITerminable
{
int Param_1 { get; set; }
}
class test<T,F> where T:ITerminable,new()
where F : Delegate,new()
{
internal delegate void Del();
internal static event Del Ev_fire;
static T a = new T();
static F b = new F();
public static void InvokeEvent()
{
Console.WriteLine("Value not added !");
}
public static void Validate()
{
if (a.Param_1 < 0.5)
{
Console.WriteLine("Value Added");
b.DynamicInvoke();
}
else
{
Ev_fire += InvokeEvent();
Ev_fire?.Invoke();
}
}
}
错误在行:Ev_fire += InvokeEvent();
试试这个:Ev_fire += InvokeEvent;
我正在尝试构建一个简单的泛型 class,它应该将对象和委托作为泛型 parameters.I 当我将方法绑定到事件时收到以下错误:
无法将类型 'void' 隐式转换为 'ConsoleApp3.test.Del'
我不明白为什么我会收到此错误,因为我的委托 returns void 并且附加到事件的方法也 returns void。
这是代码:
interface ITerminable
{
int Param_1 { get; set; }
}
class test<T,F> where T:ITerminable,new()
where F : Delegate,new()
{
internal delegate void Del();
internal static event Del Ev_fire;
static T a = new T();
static F b = new F();
public static void InvokeEvent()
{
Console.WriteLine("Value not added !");
}
public static void Validate()
{
if (a.Param_1 < 0.5)
{
Console.WriteLine("Value Added");
b.DynamicInvoke();
}
else
{
Ev_fire += InvokeEvent();
Ev_fire?.Invoke();
}
}
}
错误在行:Ev_fire += InvokeEvent();
试试这个:Ev_fire += InvokeEvent;