Inline conditional C # - the next best solution?

It seems that the compiler is not going to skip this syntax.

void main() { foo(false?0:""); } void foo(int i) {return;} void foo(string s) {return;} 

The only way I can figure this out is this:

 void bar(object o) { if (o is string){//do this} else{//im an int, do this} } 

Anyone have any better ideas?

+4
source share
8 answers

You, for example, do not make much sense (the second example does not apply to the first).

I think the first example will be fine:

 void main() { foo(""); } 

Since 0 will never be passed (false is always false), and you cannot use the inline conditional statement without assigning it somewhere (which is missing from your example).

As for the second method, perhaps that is how I would prefer to see it:

 void bar(object o) { if(o is string) foo(o as string); else foo((int)o); } 
+2
source

You cannot use a method with a return type of void in a triple expression this way. The end of the story.

To understand why this is so, remember what the ternary operator actually does - it evaluates as follows:

 (condition ? [true value] : [false value]) 

This means the following code:

 int x = a ? b : c; 

Need to rewrite :

 int x; if (a) { x = b; } else { x = c; } 

The two above are logically identical.

So how does this work with a method with void as the return type?

 // Does this make sense? int x = condition ? foo(s) : foo(i); // Or this? if (condition) { x = foo(s); } else { x = foo(i); } 

It is clear that the foregoing is not legal.

However, the suggestions of others would otherwise be valid if only your foo overloads returned the value.

In other words, if your signatures looked like this:

 object foo(string s); object foo(int i); 

Then you can do it (you throw away the return value, but at least it will be compiled):

 object o = condition ? foo(0) : foo(""); 

In any case, ol ' if / else is your best bet, in this case.

+3
source

The call to the foo method is determined at compile time, so it cannot call another method (or overload) based on the result of evaluating the condition. Instead, try something like this:

 condition ? foo(0) : foo("") 

Thus, the compiler will be able to perform overload resolution and will allow the first call to foo(int) and the second call to foo(string) .

EDIT: As noted by others, you cannot use the ?: Operator as an instruction, nor can you use methods that return void in it. If your actual methods return compatible types, you can do something like:

 int result = condition ? foo(0) : foo(""); 

If not, you should use if :

 if (condition) foo(0); else foo(""); 
+2
source

I would not pass the object as a parameter. Int will be in the box, which is slightly less efficient. Let the compiler determine which method to call.

If you wrote:

 foo(0); foo(""); 

The appropriate method will be called. You can also write:

 if (condition) { foo(0); } else { foo(""); } 

Depending on what you are trying to do (your example lacks details).

+1
source

The conditional operator requires the true and false parts to be of the same type. That is why it does not compile.

 var x = condition ? 0 : ""; 

What type should the compiler choose for x? If you really want the object to select the object, or you can force it to choose the dynamics, in this case the method overload will work, but you will lose type safety. Both, however, smell strongly. The need to test the type of runtime is usually a design error, but with limited code (which will always have the same result) it is difficult to help with another approach that will require testing on runtime types

+1
source

If you use Inline, if the expressions are in C #, both parts before and after the ":" must be of the same type. What you intend will never work.

Even if you like to do something like this:

  DateTime? theTime = true ? DateTime.Now : null; 

The compiler is not running. In this case you will have to use:

 DateTime? theTime = true ? DateTime.Now : default(DateTime?); 
+1
source

It:

 foo(false?0:"") 

Maybe so:

 false ? foo(0) : foo("") 
0
source

Both results of a conditional statement must be of the same type (or be implicitly convertible). Therefore foo(false ? 0 : "") will not work because it is trying to return Int32 and a String . Here is more information about the conditional statement.

The fix I would do is change this line to false ? foo(0) : foo("") false ? foo(0) : foo("") .

EDIT: Derp, cannot use the conditional operator only in clear text. They can only be used for appointments. You will need to use the if / else block. Not in one line, but this will be done as a last resort.

0
source

All Articles