0

NickLarsen kindley solved a problem for me couple of months ago. I should have asked him then so this truly my mistake. If NickLarsen is able to reply, thank for contacting me again, if not then if any one else can tell me the right answer most appericated it. Thanks in advance.

The previous thread is...

unable to upload multiple db images with asp.net mvc

NickLarsen used the following code and I want to know what is and why use the ? in the code. Here is the code...

    public ActionResult GetImage(int id, int? imageNum)
    {
        imageNum = imageNum ?? 0; // here

        const string alternativePicturePath = @"/Content/question_mark.jpg";
        MemoryStream stream;

        SubProductCategory4 z = db.SubProductCategory4.Where(k => k.SubProductCategoryFourID == id).FirstOrDefault();

        byte[] imageData = null;

        if (z != null)
        {
            imageData = imageNum == 1 ? z.Image1 : imageNum == 2 ? z.Image2 : imageNum == 3 ? z.Image3  : null; // here
        }

        if (imageData != null)
        {
            stream = new MemoryStream(imageData);
        }
        else
        {
            var path = Server.MapPath(alternativePicturePath);

            stream = new MemoryStream();
            var imagex = new System.Drawing.Bitmap(path);
            imagex.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            stream.Seek(0, SeekOrigin.Begin);
        }

        return new FileStreamResult(stream, "image/jpg");
    }
Community
  • 1
  • 1
DiscoDude
  • 617
  • 3
  • 17
  • 39
  • possible duplicate of [What do two question marks together mean in C#?](http://stackoverflow.com/questions/446835/what-do-two-question-marks-together-mean-in-c) – D'Arcy Rittich Jun 17 '11 at 17:34
  • possibly, it's hard to tell, in this example are the ternary operator, coallescing operator and nullable type. – taylonr Jun 17 '11 at 17:35
  • So sorry, I didn't know someone else asked similar question – DiscoDude Jun 17 '11 at 17:38

4 Answers4

3

It's the ternary operator. The syntax is:

condition ? <value if true> : <value if false>

so if imageNum is 1, imageData = z.Image1.
If it doesn't equal 1, it goes on to check the rest of the statement. In this case, the false condition has another ternary, and it checks to see if imageNum = 2, if it does, imageData will be z.Image2.

If imageNum is not 1 or 2, imageData will be null.

It's a more compact way of writing this:

if(imageNum == 1)
   imageData = z.Image1;
else if(imageNum ==2)
   imageData = z.Image2;
else
   imageData = null;

Edit The ? is actually used in 3 different ways in this method. The first is described above. The second int? says that this value is a nullable int. It can either be null or an int.

The third is called coallescing, and it looks like imageNum = imageNum ?? 0; This means that you're attempting to assign the value of imageNum to imageNum, and in the case where imageNum is null, you'll give it a default value of 0.

It's a more compact way of this:

if(imageNum == null)
   imageNum = 0;
taylonr
  • 10,732
  • 5
  • 37
  • 66
0

The ?? is in-case the view didn't provide a value for the parameter imageNum; 0 will be assigned when ImageNum is null. If you had a 1, 1 would be assigned when it is null.

Steven Striga
  • 1,371
  • 2
  • 10
  • 17
  • Thank you for view quick response. – DiscoDude Jun 17 '11 at 17:37
  • It gets even more interesting when you want to use the null coalesce syntax with value types, you get code like with '? ??' : int value = Session["anIntValue"] as int? ?? 0; – asawyer Jun 17 '11 at 17:44
0

? and ?? are branching logic. ? means make a choice between 1 and 2 1:2. ?? is essentially a shortcircuited ?. Make sense?

Gregory A Beamer
  • 16,870
  • 3
  • 25
  • 32
0
int x = y != null : 0

Breaks down into

if (y != null)
x = y;
else
x = 0;

Translated into English is"

X is equal to y unless y is null. If y is null x is equal to 0.

You can simplify this further by stating:

int x? = y ?? 0

Chase
  • 564
  • 7
  • 22