Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c# - void Func without arguments

There are some similar questions but not exactly like mine.

Is there a Func equivalent for a function without a return value (i.e. void) and without parameters?

The related question is Func not returning anything? but this does not answer for a void type.

(I need it to request actions from my view model to my view).

question from:https://stackoverflow.com/questions/9244989/void-func-without-arguments

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

1 Answer

0 votes
by (71.8m points)

Your wording is confusing. You perhaps mean "a function without a return type and no parameters." There is simply System.Action.

Action action = () => Console.WriteLine("hello world");
action();

From your comment:

But I need to fill in a <T> type in an Action and void is not a possibility (I will edit my question, I made a mistake).

This indicates a misunderstanding. The T in the Action delegate is an input. The void is the output. An Action delegate is inherently a delegate returning void. The T is the type of input it can act upon, the parameters you would then supply with arguments.

At any rate, as this answer and others show, you can have an Action delegate without any T, a delegate that takes no inputs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...