在 C# 类构造函数中作为参数委托
您好,我有一个以委托作为参数的类,如代码所示,但我收到错误 错误 1 预期类型 ...\Classes\Class1.cs 218 33 Classes
和 <代码>错误2;预期...\Classes\Class1.cs 218 96 个类。我该如何解决这个问题?提前致谢!我试图通过引用传递它,因此当类初始化时,它的某些方法会附加到委托。
public constructor(ref delegate bool delegatename(someparameters))
{
some code
}
Hi i have a class with a delegate as a parameter as shown in the code, but i get the errorsError 1 Type expected ...\Classes\Class1.cs 218 33 Classes
andError 2 ; expected ...\Classes\Class1.cs 218 96 Classes
. How do i fix the issue? Thanks in advance! I'm trying to pass it byref so when a class initializes, some method of it is attached to the delegate.
public constructor(ref delegate bool delegatename(someparameters))
{
some code
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能在构造函数中声明委托类型。您需要首先声明委托类型,然后可以在构造函数中使用它:
You cannot declare the delegate type in the constructor. You need to first declare the delegate type, and then you can use it in the constructor:
您可以传递类似
Action
的内容...但不确定为什么要通过引用传递它。例如,您可以有一个像这样的方法:并像这样调用它:
You can pass something like
Action<T>
... not sure why you want to pass it by reference though. For example, you can have a method like this one:And call it like this:
1 - 为什么使用
ref
关键字?2 -
构造函数
是类名?如果没有,你就做错了,与 PHP 不同:
public function __construct( .. ) { }
构造函数以类名命名,例如:3 - 通常委托的类型是 void 。
你在找这个吗?
然后:
输出:
1 - Why you're using the
ref
keyword?2 - the
constructor
is the class name?if not, you're doing this wrong, different of PHP:
public function __construct( .. ) { }
the constructor is named of class name, for example:3 - Normally the types of delegates are void.
You're looking for this?
Then:
The output: