开发者

MVC 3 - what is and why use the ? in a method - question related to previous thread for unable to upload multiple db images

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");
    }


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;


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜