在 C# 类构造函数中作为参数委托

发布于 2024-12-09 18:40:48 字数 309 浏览 0 评论 0原文

您好,我有一个以委托作为参数的类,如代码所示,但我收到错误 错误 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 errors
Error 1 Type expected ...\Classes\Class1.cs 218 33 Classes and
Error 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌化 2024-12-16 18:40:48

您不能在构造函数中声明委托类型。您需要首先声明委托类型,然后可以在构造函数中使用它:

public delegate bool delegatename(someparameters);

public constructor(ref delegatename mydelegate)
{
   some code...
}

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:

public delegate bool delegatename(someparameters);

public constructor(ref delegatename mydelegate)
{
   some code...
}
孤芳又自赏 2024-12-16 18:40:48

您可以传递类似 Action 的内容...但不确定为什么要通过引用传递它。例如,您可以有一个像这样的方法:

static void Foo(int x, Action<int> f) {
    f(x + 23);
}

并像这样调用它:

int x = 7;
Foo(x, p => { Console.WriteLine(p); } );

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:

static void Foo(int x, Action<int> f) {
    f(x + 23);
}

And call it like this:

int x = 7;
Foo(x, p => { Console.WriteLine(p); } );
温馨耳语 2024-12-16 18:40:48

1 - 为什么使用 ref 关键字?

2 - 构造函数是类名?
如果没有,你就做错了,与 PHP 不同: public function __construct( .. ) { } 构造函数以类名命名,例如:

class foo { 
   public foo() { } // <- class constructor 
}

3 - 通常委托的类型是 void 。

你在找这个吗?

 class Foo {

        public delegate bool del(string foo);

        public Foo(del func) { //class constructor
                int i = 0;
                while(i != 10) {
                        func(i.ToString());
                        i++;
                }
        }
    }

然后:

class App
{

    static void Main(string[] args)
    {

        Foo foo = new Foo(delegate(string n) {
                            Console.WriteLine(n);
                            return true; //this is it unnecessary, you can use the `void` type instead.          });
        Console.ReadLine();
    }
}

输出:

1
2
3
4
5
6
7
8
9

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:

class foo { 
   public foo() { } // <- class constructor 
}

3 - Normally the types of delegates are void.

You're looking for this?

 class Foo {

        public delegate bool del(string foo);

        public Foo(del func) { //class constructor
                int i = 0;
                while(i != 10) {
                        func(i.ToString());
                        i++;
                }
        }
    }

Then:

class App
{

    static void Main(string[] args)
    {

        Foo foo = new Foo(delegate(string n) {
                            Console.WriteLine(n);
                            return true; //this is it unnecessary, you can use the `void` type instead.          });
        Console.ReadLine();
    }
}

The output:

1
2
3
4
5
6
7
8
9
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文