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. quick syntax question

quick syntax question

Scheduled Pinned Locked Moved C#
question
22 Posts 6 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.
  • M Offline
    M Offline
    mprice214
    wrote on last edited by
    #1

    Hi all, Can someone let me know what is happening with the following line of code? I don't understand the syntax used in after j == (arr[i].Length - 1) Specifically what's happening with ? "" : I do know what " " is doing at the very end.

    System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");

    L M H M 4 Replies Last reply
    0
    • M mprice214

      Hi all, Can someone let me know what is happening with the following line of code? I don't understand the syntax used in after j == (arr[i].Length - 1) Specifically what's happening with ? "" : I do know what " " is doing at the very end.

      System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      the ternary operator ?: is used to choose between an empty string and a space. x?y:z is an expression that evaluates to y or to z, depending on the boolean value of x. another example would be:

      bool equals = a==b;
      Console.WriteLine(equals ? "a==b" : "a!=b");

      :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Prolific encyclopedia fixture proof-reader browser patron addict?
      We all depend on the beast below.


      1 Reply Last reply
      0
      • M mprice214

        Hi all, Can someone let me know what is happening with the following line of code? I don't understand the syntax used in after j == (arr[i].Length - 1) Specifically what's happening with ? "" : I do know what " " is doing at the very end.

        System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");

        M Offline
        M Offline
        Michel Godfroid
        wrote on last edited by
        #3

        ? is the ternary if operator. if the condition ( j == (arr[i].Length - 1) is true, it outputs"" (the null string), if the condition is false, it outputs " " (one space.

        1 Reply Last reply
        0
        • M mprice214

          Hi all, Can someone let me know what is happening with the following line of code? I don't understand the syntax used in after j == (arr[i].Length - 1) Specifically what's happening with ? "" : I do know what " " is doing at the very end.

          System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          Just in case you didn't understand the answers in the previous two replies.

          System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");

          is the same as

          string result = string.Empty;
          if (j == (arr[i].Length - 1))
          {
          result = "";
          }
          else
          {
          result = " ";
          }
          System.Console.Write("{0}{1}", arr[i][j], result);

          The ternary operator ?: is a shorthand version of if - else. See here[^].

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          L 1 Reply Last reply
          0
          • H Henry Minute

            Just in case you didn't understand the answers in the previous two replies.

            System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");

            is the same as

            string result = string.Empty;
            if (j == (arr[i].Length - 1))
            {
            result = "";
            }
            else
            {
            result = " ";
            }
            System.Console.Write("{0}{1}", arr[i][j], result);

            The ternary operator ?: is a shorthand version of if - else. See here[^].

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

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

            What happens with the sequence points though? For the ternary operator there is a sequence point between the condition and the second/third operand (whichever it chooses), but AFAIK not after the second/third operand (unless you close it with a semicolon, of course) Or did MS sneakily add some sequence points there, like they did with method arguments?

            H L 2 Replies Last reply
            0
            • L Lost User

              What happens with the sequence points though? For the ternary operator there is a sequence point between the condition and the second/third operand (whichever it chooses), but AFAIK not after the second/third operand (unless you close it with a semicolon, of course) Or did MS sneakily add some sequence points there, like they did with method arguments?

              H Offline
              H Offline
              Henry Minute
              wrote on last edited by
              #6

              It is beyond my capabilities to give a definitive answer to that although I suspect that Luc might be able to. As a guess though, and knowing how sneaky M$ are, I'd go for the latter. :)

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              1 Reply Last reply
              0
              • M mprice214

                Hi all, Can someone let me know what is happening with the following line of code? I don't understand the syntax used in after j == (arr[i].Length - 1) Specifically what's happening with ? "" : I do know what " " is doing at the very end.

                System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");

                M Offline
                M Offline
                mprice214
                wrote on last edited by
                #7

                Thanks everyone. I googled ?:, but didn't see anything and am not experienced enough to have looked at msdn's operators. Will know for next time.

                1 Reply Last reply
                0
                • L Lost User

                  What happens with the sequence points though? For the ternary operator there is a sequence point between the condition and the second/third operand (whichever it chooses), but AFAIK not after the second/third operand (unless you close it with a semicolon, of course) Or did MS sneakily add some sequence points there, like they did with method arguments?

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Disclaimer: I don't know the finer details as I don't really care; with 20+ years of C under the belt (using all kinds of compilers) I have learned to stay well away from the borders. Defensive programming is what I call it. Besides, doing so improves readability and robustness of the code. But then I decided to browse the C# 3.0 spec anyway, and here are some findings: 1. the notion of "sequence point" does not appear in the C# standard. 2. "The order of evaluation of operators in an expression is determined by the precedence and associativity of the operators (§7.2.1)." 3. "If the array creation expression contains an array initializer, then each expression in the array initializer is evaluated and assigned to its corresponding array element. The evaluations and assignments are performed in the order the expressions are written in the array initializer" 4. "A method decorated with the Conditional attribute is a conditional method. The Conditional attribute indicates a condition by testing a conditional compilation symbol. Calls to a conditional method are either included or omitted depending on whether this symbol is defined at the point of the call. If the symbol is defined, the call is included; otherwise, the call (including evaluation of the parameters of the call) is omitted." I did not find: - a single thing about side-effects - evaluation order of method call arguments which does not mean it isn't covered, there are over 500 pages in the spec. Note: as 4.0 supports named arguments (and hence accepts them in any order), the problem potentially gets larger. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  Prolific encyclopedia fixture proof-reader browser patron addict?
                  We all depend on the beast below.


                  modified on Monday, April 26, 2010 3:32 PM

                  M L 3 Replies Last reply
                  0
                  • L Luc Pattyn

                    Disclaimer: I don't know the finer details as I don't really care; with 20+ years of C under the belt (using all kinds of compilers) I have learned to stay well away from the borders. Defensive programming is what I call it. Besides, doing so improves readability and robustness of the code. But then I decided to browse the C# 3.0 spec anyway, and here are some findings: 1. the notion of "sequence point" does not appear in the C# standard. 2. "The order of evaluation of operators in an expression is determined by the precedence and associativity of the operators (§7.2.1)." 3. "If the array creation expression contains an array initializer, then each expression in the array initializer is evaluated and assigned to its corresponding array element. The evaluations and assignments are performed in the order the expressions are written in the array initializer" 4. "A method decorated with the Conditional attribute is a conditional method. The Conditional attribute indicates a condition by testing a conditional compilation symbol. Calls to a conditional method are either included or omitted depending on whether this symbol is defined at the point of the call. If the symbol is defined, the call is included; otherwise, the call (including evaluation of the parameters of the call) is omitted." I did not find: - a single thing about side-effects - evaluation order of method call arguments which does not mean it isn't covered, there are over 500 pages in the spec. Note: as 4.0 supports named arguments (and hence accepts them in any order), the problem potentially gets larger. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    Prolific encyclopedia fixture proof-reader browser patron addict?
                    We all depend on the beast below.


                    modified on Monday, April 26, 2010 3:32 PM

                    M Offline
                    M Offline
                    Michel Godfroid
                    wrote on last edited by
                    #9

                    I don't think there can be any side effects in C#, because objects (and everything is an object), are essentially static in nature. For example in C, an operation like x++?: could introduce some ambiguity, while in C#, x and x++ are distinct objects. Am I out of my depth here? Absolutely :-)

                    L 2 Replies Last reply
                    0
                    • M Michel Godfroid

                      I don't think there can be any side effects in C#, because objects (and everything is an object), are essentially static in nature. For example in C, an operation like x++?: could introduce some ambiguity, while in C#, x and x++ are distinct objects. Am I out of my depth here? Absolutely :-)

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      There always is some potential for side-effects. First example:

                      int v = ...;
                      int y = (v++==100) ? 0 : v;

                      will the auto-increment happen before or after the evaluation of the second/third operand? Second example:

                      int y = someFlag ? method1() : method2();

                      We all hope this causes exactly one method to get executed; in general it is not equivalent to:

                      int v1=method1();
                      int v2=method2();
                      int y = someFlag ? v1 : v2;

                      as those methods could have all kinds of side-effects. However I did not see that stated explicitly in the spec. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      Prolific encyclopedia fixture proof-reader browser patron addict?
                      We all depend on the beast below.


                      N 1 Reply Last reply
                      0
                      • M Michel Godfroid

                        I don't think there can be any side effects in C#, because objects (and everything is an object), are essentially static in nature. For example in C, an operation like x++?: could introduce some ambiguity, while in C#, x and x++ are distinct objects. Am I out of my depth here? Absolutely :-)

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        Someone disliked your message enough to down-vote it. The topic is interesting enough IMO, albeit a little OT but that's OK. :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                        Prolific encyclopedia fixture proof-reader browser patron addict?
                        We all depend on the beast below.


                        1 Reply Last reply
                        0
                        • L Luc Pattyn

                          There always is some potential for side-effects. First example:

                          int v = ...;
                          int y = (v++==100) ? 0 : v;

                          will the auto-increment happen before or after the evaluation of the second/third operand? Second example:

                          int y = someFlag ? method1() : method2();

                          We all hope this causes exactly one method to get executed; in general it is not equivalent to:

                          int v1=method1();
                          int v2=method2();
                          int y = someFlag ? v1 : v2;

                          as those methods could have all kinds of side-effects. However I did not see that stated explicitly in the spec. :)

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                          Prolific encyclopedia fixture proof-reader browser patron addict?
                          We all depend on the beast below.


                          N Offline
                          N Offline
                          Not Active
                          wrote on last edited by
                          #12

                          Luc Pattyn wrote:

                          will the auto-increment happen before or after the evaluation of the second/third operand?

                          Neither, because the code won't compile. "int v = ...;" ;P


                          I know the language. I've read a book. - _Madmatt

                          L 1 Reply Last reply
                          0
                          • N Not Active

                            Luc Pattyn wrote:

                            will the auto-increment happen before or after the evaluation of the second/third operand?

                            Neither, because the code won't compile. "int v = ...;" ;P


                            I know the language. I've read a book. - _Madmatt

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #13

                            you're right, you'd better insert some expression that the compiler can't evaluate right away. :)

                            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                            Prolific encyclopedia fixture proof-reader browser patron addict?
                            We all depend on the beast below.


                            1 Reply Last reply
                            0
                            • L Luc Pattyn

                              Disclaimer: I don't know the finer details as I don't really care; with 20+ years of C under the belt (using all kinds of compilers) I have learned to stay well away from the borders. Defensive programming is what I call it. Besides, doing so improves readability and robustness of the code. But then I decided to browse the C# 3.0 spec anyway, and here are some findings: 1. the notion of "sequence point" does not appear in the C# standard. 2. "The order of evaluation of operators in an expression is determined by the precedence and associativity of the operators (§7.2.1)." 3. "If the array creation expression contains an array initializer, then each expression in the array initializer is evaluated and assigned to its corresponding array element. The evaluations and assignments are performed in the order the expressions are written in the array initializer" 4. "A method decorated with the Conditional attribute is a conditional method. The Conditional attribute indicates a condition by testing a conditional compilation symbol. Calls to a conditional method are either included or omitted depending on whether this symbol is defined at the point of the call. If the symbol is defined, the call is included; otherwise, the call (including evaluation of the parameters of the call) is omitted." I did not find: - a single thing about side-effects - evaluation order of method call arguments which does not mean it isn't covered, there are over 500 pages in the spec. Note: as 4.0 supports named arguments (and hence accepts them in any order), the problem potentially gets larger. :)

                              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                              Prolific encyclopedia fixture proof-reader browser patron addict?
                              We all depend on the beast below.


                              modified on Monday, April 26, 2010 3:32 PM

                              M Offline
                              M Offline
                              Michel Godfroid
                              wrote on last edited by
                              #14

                              Luc Pattyn wrote:

                              I have learned to stay well away from the borders. Defensive programming is what I call it.

                              Couldn't agree more, and liberal use of parentheses :-)

                              1 Reply Last reply
                              0
                              • L Luc Pattyn

                                Disclaimer: I don't know the finer details as I don't really care; with 20+ years of C under the belt (using all kinds of compilers) I have learned to stay well away from the borders. Defensive programming is what I call it. Besides, doing so improves readability and robustness of the code. But then I decided to browse the C# 3.0 spec anyway, and here are some findings: 1. the notion of "sequence point" does not appear in the C# standard. 2. "The order of evaluation of operators in an expression is determined by the precedence and associativity of the operators (§7.2.1)." 3. "If the array creation expression contains an array initializer, then each expression in the array initializer is evaluated and assigned to its corresponding array element. The evaluations and assignments are performed in the order the expressions are written in the array initializer" 4. "A method decorated with the Conditional attribute is a conditional method. The Conditional attribute indicates a condition by testing a conditional compilation symbol. Calls to a conditional method are either included or omitted depending on whether this symbol is defined at the point of the call. If the symbol is defined, the call is included; otherwise, the call (including evaluation of the parameters of the call) is omitted." I did not find: - a single thing about side-effects - evaluation order of method call arguments which does not mean it isn't covered, there are over 500 pages in the spec. Note: as 4.0 supports named arguments (and hence accepts them in any order), the problem potentially gets larger. :)

                                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                                Prolific encyclopedia fixture proof-reader browser patron addict?
                                We all depend on the beast below.


                                modified on Monday, April 26, 2010 3:32 PM

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

                                I didn't find much, but I did find "The expressions of an argument list are always evaluated in the order they are written. " on page 182/553 of the C# 2 spec (4th edition / June 2006) I'm not sure whether that still applies to C# 4 - I'll download the new spec (I don't really know why I still had that old one) And in 14.2 (page 172) of the same document I found "The order in which operands in an expression are evaluated, is left to right. [Example: In F(i) + G(i++) * H(i), method F is called using the old value of i, then method G is called with the old value of i, and, finally, method H is called with the new value of i. This is separate from and unrelated to operator precedence. end example] " edit: IMO that [the first quote] supports my claim that MS had inserted sequence points in method argument evaluation, they're just not calling it that..

                                L 1 Reply Last reply
                                0
                                • L Lost User

                                  I didn't find much, but I did find "The expressions of an argument list are always evaluated in the order they are written. " on page 182/553 of the C# 2 spec (4th edition / June 2006) I'm not sure whether that still applies to C# 4 - I'll download the new spec (I don't really know why I still had that old one) And in 14.2 (page 172) of the same document I found "The order in which operands in an expression are evaluated, is left to right. [Example: In F(i) + G(i++) * H(i), method F is called using the old value of i, then method G is called with the old value of i, and, finally, method H is called with the new value of i. This is separate from and unrelated to operator precedence. end example] " edit: IMO that [the first quote] supports my claim that MS had inserted sequence points in method argument evaluation, they're just not calling it that..

                                  L Offline
                                  L Offline
                                  Luc Pattyn
                                  wrote on last edited by
                                  #16

                                  Thanks Harold for "The expressions of an argument list are always evaluated in the order they are written"; that is what I knew had to be there, and I now found it in C# 3.0 too. I'm afraid there is some semantic ambiguity involved; to evaluate can either be "determine the value" or "process the expression yielding a value and execute all side-effects", and I'm pretty sure the latter is what was meant for argument lists.

                                  harold aptroot wrote:

                                  And in 14.2 (page 172)

                                  the numbers clearly have changed since. I now found that sentence too, it is under 7.2; and it supports the broader meaning of "to evaluate". :)

                                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                                  Prolific encyclopedia fixture proof-reader browser patron addict?
                                  We all depend on the beast below.


                                  L 1 Reply Last reply
                                  0
                                  • L Luc Pattyn

                                    Thanks Harold for "The expressions of an argument list are always evaluated in the order they are written"; that is what I knew had to be there, and I now found it in C# 3.0 too. I'm afraid there is some semantic ambiguity involved; to evaluate can either be "determine the value" or "process the expression yielding a value and execute all side-effects", and I'm pretty sure the latter is what was meant for argument lists.

                                    harold aptroot wrote:

                                    And in 14.2 (page 172)

                                    the numbers clearly have changed since. I now found that sentence too, it is under 7.2; and it supports the broader meaning of "to evaluate". :)

                                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                                    Prolific encyclopedia fixture proof-reader browser patron addict?
                                    We all depend on the beast below.


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

                                    The second meaning is also what's used by the compiler, so it's probably that :) But I can't really find any good "C# 4 language specification" documents, do you know one? Google didn't seem in the mood to give me useful results.. I found some drafts, but well, they're drafts.. or does that mean that the C# 4 specs aren't "official" yet?

                                    modified on Monday, April 26, 2010 5:31 PM

                                    L 3 Replies Last reply
                                    0
                                    • L Lost User

                                      The second meaning is also what's used by the compiler, so it's probably that :) But I can't really find any good "C# 4 language specification" documents, do you know one? Google didn't seem in the mood to give me useful results.. I found some drafts, but well, they're drafts.. or does that mean that the C# 4 specs aren't "official" yet?

                                      modified on Monday, April 26, 2010 5:31 PM

                                      L Offline
                                      L Offline
                                      Luc Pattyn
                                      wrote on last edited by
                                      #18

                                      This[^] is what Google is willing to offer; I see two candidates, nothing final though. :)

                                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                                      Prolific encyclopedia fixture proof-reader browser patron addict?
                                      We all depend on the beast below.


                                      M 1 Reply Last reply
                                      0
                                      • L Lost User

                                        The second meaning is also what's used by the compiler, so it's probably that :) But I can't really find any good "C# 4 language specification" documents, do you know one? Google didn't seem in the mood to give me useful results.. I found some drafts, but well, they're drafts.. or does that mean that the C# 4 specs aren't "official" yet?

                                        modified on Monday, April 26, 2010 5:31 PM

                                        L Offline
                                        L Offline
                                        Luc Pattyn
                                        wrote on last edited by
                                        #19

                                        harold aptroot wrote:

                                        the C# 4 specs aren't "official" yet?

                                        yes, I expect that is part of the game: propose a standard, get it accepted, then propose extensions and implement them faster than anyone else can, giving you de facto control. :)

                                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                                        Prolific encyclopedia fixture proof-reader browser patron addict?
                                        We all depend on the beast below.


                                        1 Reply Last reply
                                        0
                                        • L Luc Pattyn

                                          This[^] is what Google is willing to offer; I see two candidates, nothing final though. :)

                                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                                          Prolific encyclopedia fixture proof-reader browser patron addict?
                                          We all depend on the beast below.


                                          M Offline
                                          M Offline
                                          Michel Godfroid
                                          wrote on last edited by
                                          #20

                                          Google is NOT your friend :-) http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=dfbf523c-f98c-4804-afbd-459e846b268e[^] But I haven't got the impression that it's different from 3.0 regarding the ternary operator.

                                          L 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