0

Following is a String interpolation example. If apple is equals to apple it returns yes or else no.

var result = $"{("apple" == "apple" ? "yes" : "no")}";

What I need to do is to assign a string

string text= "{(apple == apple ? yes: no)}";
var result = $"{text}";

I expected var result will be yes. As its similar to the above code. However, the result I get is the string text it self.

My question: How can I pass a string similar to "{(apple == apple ? yes: no)}"and get an output like yes or no ?

6
  • You can't, at least not with that specific language feature. It doesn't work like that. Commented Nov 13, 2018 at 17:19
  • @Meta-Knight it is {(apple == apple ? yes: no)} (May be I should have rephased it as the output of the variable text). Commented Nov 13, 2018 at 17:19
  • 1
    Possible duplicate of Is it possible to pass interpolated strings as parameter to a method? Commented Nov 13, 2018 at 17:19
  • Is there any other way i could achieve this ? Commented Nov 13, 2018 at 17:28
  • See the proposed duplicate. I think you may be able to get something working with FormattableString but I haven't personally used it yet. Commented Nov 13, 2018 at 17:29

4 Answers 4

2

If all you wanted was to return "yes" or "no" then you wouldn't need interpolation because there's nothing to interpolate. You could just do this:

var result = something == "apple" ? "yes" : "no";

If you needed some concatenation - in other words, you had to interpolate something - you could do this:

var something = "apple";
var result = $"Is it an apple? {(something == "apple" ? "yes" : "no")}!";

Returns

Is it an apple? yes!

The entire conditional expression needs to be in parenthesis.

For readability it might make sense to break it up into steps anyway.

var something = "apple";
var yesOrNo = something == "apple" ? "yes" : "no";
var result = $"Is it an apple? {yesOrNo}!";
Sign up to request clarification or add additional context in comments.

2 Comments

What if the input string is ONLY "{(apple == apple ? yes: no)}". And, I need to output the answer YES or NO. Do I have to split the string and construct an Equation as shown in your example ? Or is there any other way I could do this ?
In that case you would be trying to place code inside of a string literal and interpret it at runtime. Such a thing exists but it's way off the beaten path, like trying to generate classes dynamically at runtime. Unless you're trying to build a compiler then it's almost certainly something that you wouldn't want to do.
1

String interpolation with $ does not take place at run time. It is evaluated at compile time. Only the expression between the curly braces is evaluated at run time.

When you want to do the evaluation later at run time you need to use a delegate:

Func<string> textfactory = () => "apple" == "apple" ? "yes" : "no";
var result = $"{ textfactory() }";

or just

var result = textfactory();

since there is nothing to interpolate but the string itself.

The delegate can be passed around, e.g. as function argument.

Comments

0

Can't. Sense of interpolation - allow to inject output of code inside string. Compiler should know what to inject. In second case string =text= can contain anything, it's more for interpreter, not compiler. Now code work way it should. Imho.

Comments

0

This isn't the same syntax that you were looking for, but this works:

 public class yourFruitStand
 {
      private string yourFruitName;
      public bool YourFruitName
      {
           get
           {
               return yourFruitName;
           }
           set
           {
               yourFruitName = value;

               if(yourFruitName == "apple")
               {
                    yesOrNo = true;
               }
               else
               {
                    yesOrNo = false;
               }
           }
      }
      private bool yesOrNo;
      public bool yesOrNo
      {
           get
           {
               return yesOrNo;
           }
           set
           {
               yesOrNo = value;
           }
      }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.