2

At first I tried to write an If-Then-Else statement using a ternary operator.
It works fine then Just out of curiosity I decided to write the same code using a
null-coalescing operator but it doesn't work as expected.

public System.Web.Mvc.ActionResult MyAction(int? Id)
{
    string MyContetnt = string.Empty;

    //This line of code works perfectly
    //MyContent = Id.HasValue ? Id.Value.ToString() : "Id has no value";

    //This line of code dosent show "Id has no value" at all 
    MyContetnt = (System.Convert.ToString(Id) ?? "Id has no value").ToString();

    return Content(MyContetnt);
}

If I run the program through the route Mysite/Home/MyAction/8777 everything is perfect and the entered Id number will be shown.

But if I run the program without any Id through the route MySite/Home/MyAction nothing will happen and MyContetnt will be empty whereas I expect to see "Id has no value" on the screen.

Am I missing something?

Edit: I am curious that Is it possible to write the code by using ?? ( null coalescing operator )?

siamak
  • 669
  • 1
  • 10
  • 28

2 Answers2

6

Convert.ToString() results in an Empty string when the conversion has failed. So the null coalescing operator won't detect a null but an Empty string

You should use:

MyContetnt = Id.HasValue ? System.Convert.ToString(Id.Value) : "Id has no value";
dcastro
  • 66,540
  • 21
  • 145
  • 155
middelpat
  • 2,555
  • 1
  • 20
  • 29
  • `System.Convert.ToString(Id.Value)` can be simplified to `Id.ToString()` – Sriram Sakthivel May 27 '14 at 08:58
  • Correct. But since he was allready using Convert.ToString() I chose to keep it that way – middelpat May 27 '14 at 08:59
  • @Sriram Sakthivel the Id.toString() or Id.value.toString() raises an InvalidOperationException when the Id or Id.value is null – siamak May 27 '14 at 09:02
  • @siamak That's why I use the ? : statement (shorthand or inline If statement) to ensure Id is not null when calling .ToString or Convert.ToString – middelpat May 27 '14 at 09:04
  • 1
    @siamak I know, You should note there is a check against `Id.HasValue` exist in middelpat's answer. – Sriram Sakthivel May 27 '14 at 09:04
  • @middelpat : if i understand you Correctly you mean that the result of system.Convert.TosString(Id.vaalue) would be an Empty string not null, so the output would be an Empty string"", is there any workaround to write the code by the ?? operator ? – siamak May 27 '14 at 09:08
  • @siamak As far as I know the answer I suggested should be the best approach for this problem. There is no need to use the ?? instead of the ? : operators – middelpat May 27 '14 at 09:11
  • @middelpat : thank you for your good point but As I mentioned in the question I know how to do it by the ternary operator ,MyContent = Id.HasValue ? Id.Value.ToString() : "Id has no value"; but i was interested in writing it by using ?? operator . – siamak May 27 '14 at 09:19
  • @siamak In this case that wouldn't be possible without creating an unessasary, unlogical, realy ugly code construction which would be just a workaround to be able to use the ?? operator. Which wouldn't make sense at all – middelpat May 28 '14 at 12:13
2

Convert.ToString(), when acting on an int? will produce either a numerical string (if the int? has a value) or an empty string (otherwise).

Therefore, it's not producing null, it's producing "", and "" ?? x == "".

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251