Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. a ? b : c?

a ? b : c?

Scheduled Pinned Locked Moved C#
question
9 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bfis108137
    wrote on last edited by
    #1

    What does this do? I haven't been able to find a suitable explanation as to what it does and i found it in some code that someone gave me.

    M L P L realJSOPR 6 Replies Last reply
    0
    • B bfis108137

      What does this do? I haven't been able to find a suitable explanation as to what it does and i found it in some code that someone gave me.

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      i think its something along the lines of: If a is true, then result is b, otherwise result is c

      1 Reply Last reply
      0
      • B bfis108137

        What does this do? I haven't been able to find a suitable explanation as to what it does and i found it in some code that someone gave me.

        L Offline
        L Offline
        Le centriste
        wrote on last edited by
        #3
        x = a ? b : c;
        

        is the shorthand version of

        if (a)
        {
            x = b;
        }
        else
        {
            x = c;
        }
        
        L 1 Reply Last reply
        0
        • B bfis108137

          What does this do? I haven't been able to find a suitable explanation as to what it does and i found it in some code that someone gave me.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          It's known as a ternary operator. Basically, this is an operator that reads like this: "If the condition is true then use the value immediately after the question mark, otherwise use the value after the colon". Another operator you may sometimes see is the ?? operator. This is known as the coalescing null operator, and is used in statements like this:

          MyClass a = value1 ?? value2 ?? new MyClass();

          This reads like this "MyClass a is assigned value1, unless value1 is null in which case, it will use value2, unless value2 is null in which cass it uses new MyClass();" This is an efficient way to assign a default value if a null value is encountered.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          My blog | My articles | MoXAML PowerToys

          1 Reply Last reply
          0
          • L Le centriste
            x = a ? b : c;
            

            is the shorthand version of

            if (a)
            {
                x = b;
            }
            else
            {
                x = c;
            }
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Except that ?: counts as an expression, instead of a statement. So there are more places where you're allowed to use it. Such as:

            if (a?b:c) { /* some code */ }

            or

            int x = a?b:c;

            (that doesn't mean you're wrong, I just think it might help bfis108137 to know that)

            1 Reply Last reply
            0
            • B bfis108137

              What does this do? I haven't been able to find a suitable explanation as to what it does and i found it in some code that someone gave me.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              see Ternary Operation[^], and ?:[^] specifically.

              1 Reply Last reply
              0
              • B bfis108137

                What does this do? I haven't been able to find a suitable explanation as to what it does and i found it in some code that someone gave me.

                realJSOPR Offline
                realJSOPR Offline
                realJSOP
                wrote on last edited by
                #7

                It's called a ternary operation, and is the shortcut of doing this:

                int z = 0;
                int x = 1;
                int y = 2;
                if (x==y)
                {
                z = x;
                }
                else
                {
                z = y;
                }

                // in the format you cited:

                z = (x ==y) ? x : y;

                Many high-browed programmers don't like it, but as long as you keep it to just one comparison instead of stacking them, I think it's an acceptable coding practice.

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                P 1 Reply Last reply
                0
                • realJSOPR realJSOP

                  It's called a ternary operation, and is the shortcut of doing this:

                  int z = 0;
                  int x = 1;
                  int y = 2;
                  if (x==y)
                  {
                  z = x;
                  }
                  else
                  {
                  z = y;
                  }

                  // in the format you cited:

                  z = (x ==y) ? x : y;

                  Many high-browed programmers don't like it, but as long as you keep it to just one comparison instead of stacking them, I think it's an acceptable coding practice.

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Right, it should not be abused; I would not use it as z = (x ==y) ? x : y;, because an if statement would work for that. One situation I use it in is something like this:

                  string.Format
                  (
                  "{0} record{1} processed."
                  ,
                  count
                  ,
                  count==1?"":"s"
                  ) ;

                  I find this more readable than the alternatives. Whenever I see an application write something like, "1 records processed", I :sigh: . Come on, guys, use the ternary operator!

                  1 Reply Last reply
                  0
                  • B bfis108137

                    What does this do? I haven't been able to find a suitable explanation as to what it does and i found it in some code that someone gave me.

                    N Offline
                    N Offline
                    Najmal
                    wrote on last edited by
                    #9

                    hi... If "a" is true then return value is "b" else return value is "c" EXAMPLE:: int nLargeNo=0, nFirstNo=100,nSecondNo=200; nLargeNo = (nFirstNo>nSecondNo)?nFirstNo:nSecondNo; Answer: value of nLargeNo is 200

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups