Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

c# - Can you use the params keyword in a delegate?

I'd like to define a delegate that takes a couple of dates, an unknown number of other parameters (using the params keyword), and that returns a list of objects:

Func<DateTime, DateTime, params int[], List<object>>

Visual Studio doesn't like the syntax which is making me think this isn't allowed. Can anyone tell me why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't use params for any parameter other than the last one... that's part of what it's complaining about.

You also can't use params in a type argument. This isn't just for delegates, but in general. For example, you can't write:

List<params string[]> list = new List<params string[]>();

You can, however, declare a new delegate type, like this:

delegate void Foo(int x, params string[] y);

...

Foo foo = SomeMethod;
foo(10, "Hi", "There");

Note that the method group conversion will have to match a method which takes a string array - you couldn't declare SomeMethod as:

void SomeMethod(int x, string a, string b)

and expect the above to work, for example. It would have to be:

void SomeMethod(int x, string[] args)

(Or it could use params itself, of course.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...