English 中文(简体)
作为C#级建筑群参数的代表
原标题:Delegate as parameter in C# class constructor

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 ...ClassesClass1.cs 218 33 Classes and Error 2 ; expected ...ClassesClass1.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
}
最佳回答

您不能宣布代表的类型。 您必须首先宣布代表的类型,然后由您在构造中使用:

public delegate bool delegatename(someparameters);

public constructor(ref delegatename mydelegate)
{
   some code...
}
问题回答

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);
}

并称之为:

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

页: 1

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.

请重新研究这一问题?

 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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签