Using conditional (?:) operator for method selection in C# (3.0)?

后端 未结 7 1971
日久生厌
日久生厌 2020-12-01 11:49

I\'m refactoring some code.

Right now there are quite a few places with functions like this:

string error;
if (a) {
   error = f1(a, long, parameter,         


        
相关标签:
7条回答
  • 2020-12-01 12:53

    No, basically, without making it less efficient. If there is a return value, you can use:

    var result = cond ? methodA(a,b,c,d) : methodB(a,b,c,d);
    

    but that is about it.

    Well, you can create a pair of delegates from the method group, but that adds overhead for no good reason. I won't endorse it.

    0 讨论(0)
提交回复
热议问题