Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use void method with MapResult C#

I am trying to use void methods with MapResult method in Main() method in console application. Following is my code, Class1, Class2 and Class3 inherits AbstractClass and implements it's Execute() method:

Parser.Default.ParseArguments<
                        Class1, 
                        Class2,
                        Class3>(command.Split(' '))
                      .MapResult((AbstractClass o) => o.Execute(), err => null);

In the above code, my Execute() method returns void. I receive following error:

The type arguments for method 'ParserResultExtensions.MapResult(ParserResult, Func, Func, TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

However, if I change return type of Execute() method to int, it works fine.

Please suggest me some solution to use void returning method with MapResult.

like image 554
rushil bhuptani Avatar asked Feb 10 '18 10:02

rushil bhuptani


1 Answers

Assuming you are using CommandLineParser (https://github.com/commandlineparser/commandline), MapResult requires you to provide a return value indicating success/failure. Try this:

.MapResult((AbstractClass o) => {o.Execute(); return 1;}, err => null);
like image 155
Code Maverick Avatar answered Sep 30 '22 04:09

Code Maverick