Easy Syntax Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terry McNamee

    Easy Syntax Question

    return arr.Length == 0 ? null : arr[item].ToString();



    Can anyone tell me what this syntax means?

    Does it basically mean if arr.Length is equal to 0 then return null, else
    return the item in the array ??


  • Bruce Wood

    #2
    Re: Easy Syntax Question

    Yes. It is equivalent to:

    if (arr.Length == 0)
    {
    return null;
    }
    else
    {
    return arr[item].ToString();
    }

    BTW, I hate the "ternary" operator. I hated it in C, I hated it in C++,
    and now I hate it in C#, too. There's only one place in C# where you
    absolutely can't do without it, and that's when chaining constructor
    calls, where you're not allowed to insert "if" statements:

    MyClass(string foo) : base(foo == null : "" ? foo)

    Otherwise, it's just a throwback to the days of "look how much code I
    can stuff onto one line". Ugly and difficult to maintain, IMHO.

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Easy Syntax Question

      <"Terry McNamee" <n/a>> wrote:[color=blue]
      > return arr.Length == 0 ? null : arr[item].ToString();
      >
      > Can anyone tell me what this syntax means?
      >
      > Does it basically mean if arr.Length is equal to 0 then return null, else
      > return the item in the array ??[/color]

      Exactly. It's a conditional operator. Basically, the condition
      (leftmost operand) is evaluated, and if it evaluates to true, the
      middle operand is evaluated and is the result of the expression.
      Otherwise, the rightmost operand is evaluated and is the result of the
      expression.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Easy Syntax Question

        Terry,

        For the most part, yes. However, to be correct, it should be:

        return ((arr == null || arr.Length == 0) ? null : arr[item].ToString());

        This assumes that arr might be null.

        Hope this helps.

        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Terry McNamee" <n/a> wrote in message
        news:%23aIREX6Z FHA.1152@tk2msf tngp13.phx.gbl. ..[color=blue]
        > return arr.Length == 0 ? null : arr[item].ToString();
        >
        >
        >
        > Can anyone tell me what this syntax means?
        >
        > Does it basically mean if arr.Length is equal to 0 then return null, else
        > return the item in the array ??
        >
        >[/color]


        Comment

        • Terry McNamee

          #5
          Re: Easy Syntax Question

          Thanks Guys. :-)


          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
          message news:uu5lqd6ZFH A.2984@TK2MSFTN GP15.phx.gbl...[color=blue]
          > Terry,
          >
          > For the most part, yes. However, to be correct, it should be:
          >
          > return ((arr == null || arr.Length == 0) ? null : arr[item].ToString());
          >
          > This assumes that arr might be null.
          >
          > Hope this helps.
          >
          > --
          > - Nicholas Paldino [.NET/C# MVP]
          > - mvp@spam.guard. caspershouse.co m
          >
          > "Terry McNamee" <n/a> wrote in message
          > news:%23aIREX6Z FHA.1152@tk2msf tngp13.phx.gbl. ..[color=green]
          >> return arr.Length == 0 ? null : arr[item].ToString();
          >>
          >>
          >>
          >> Can anyone tell me what this syntax means?
          >>
          >> Does it basically mean if arr.Length is equal to 0 then return null, else
          >> return the item in the array ??
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • C# Learner

            #6
            Re: Easy Syntax Question

            Bruce Wood <brucewood@cana da.com> wrote:

            [the ternary operator]
            [color=blue]
            > Otherwise, it's just a throwback to the days of "look how much code I
            > can stuff onto one line". Ugly and difficult to maintain, IMHO.[/color]

            While it's certainly possible to abuse it (as is the case with some other
            language features), there are situations where it helps make the code nicer
            and more readable, IMO.

            For example, I find

            string s = foo ? "foo" : "not foo";

            much nicer and more readable than

            string s;
            if (foo) {
            s = "foo";
            } else {
            s = "not foo";
            }

            Using the 'if .. else' construct here is overkill in comparison, IMO.

            Comment

            • Bill Butler

              #7
              Re: Easy Syntax Question

              "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
              message news:uu5lqd6ZFH A.2984@TK2MSFTN GP15.phx.gbl...[color=blue]
              > Terry,
              >
              > For the most part, yes. However, to be correct, it should be:
              >
              > return ((arr == null || arr.Length == 0) ? null : arr[item].ToString());
              >
              > This assumes that arr might be null.
              >[/color]

              I'll Top that :^)
              return ((arr == null || arr.Length <= item) ? null : arr[item].ToString());

              That way we don't index outside the Array. (unless you like exceptions)

              Bill


              Comment

              • Jonathan Allen

                #8
                Re: Easy Syntax Question

                > While it's certainly possible to abuse it

                My favorite example from real life:
                bool var = (a != null ) ? true : false;
                [color=blue]
                > if (foo) {
                > s = "foo";
                > } else {
                > s = "not foo";
                > }[/color]

                That can be cut down quite a bit using the If-Then-Else pattern from
                FORTRAN/BASIC. Still, not quite as good as it could be.

                if (foo) s = "foo"; else s = "not foo";

                --
                Jonathan Allen


                "C# Learner" <csharp@learner .here> wrote in message
                news:14g0xtem8o 5ri.dlg@csharp. learner...[color=blue]
                > Bruce Wood <brucewood@cana da.com> wrote:
                >
                > [the ternary operator]
                >[color=green]
                >> Otherwise, it's just a throwback to the days of "look how much code I
                >> can stuff onto one line". Ugly and difficult to maintain, IMHO.[/color]
                >
                > While it's certainly possible to abuse it (as is the case with some other
                > language features), there are situations where it helps make the code
                > nicer
                > and more readable, IMO.
                >
                > For example, I find
                >
                > string s = foo ? "foo" : "not foo";
                >
                > much nicer and more readable than
                >
                > string s;
                > if (foo) {
                > s = "foo";
                > } else {
                > s = "not foo";
                > }
                >
                > Using the 'if .. else' construct here is overkill in comparison, IMO.[/color]


                Comment

                • C# Learner

                  #9
                  Re: Easy Syntax Question

                  Jonathan Allen <x@x.x> wrote:
                  [color=blue]
                  > if (foo) s = "foo"; else s = "not foo";[/color]

                  Still, to go with my example it'd have to be:

                  string s;
                  if (foo) s = "foo"; else s = "not foo";

                  I'd take

                  string s = foo ? "foo" : "not foo";

                  over that anytime. :-) It's clearer and nicer (IMO), and it doesn't
                  require you to break your coding style temporarily.

                  Comment

                  • Bruce Wood

                    #10
                    Re: Easy Syntax Question

                    My problem with the ternary operator has always been a result of my
                    work environment: I've always worked in multi-language shops. When
                    you're working shoulder-to-shoulder with COBOL programmers and VB
                    programmers, you're better off writing

                    if (foo)
                    {
                    s = "foo";
                    }
                    else
                    {
                    s = "not foo";
                    }

                    even though it's much wordier. That way, when the guy next to you has
                    to take over maintaining the C code for a week while you're on
                    vacation, he has a much easier time of it, even if he doesn't know much
                    C.

                    If you work in a C#-only (or C-only or C++-only) shop, then I see no
                    problem with the ternary operator. In my work environment, however,
                    it's just one more mysterious C-ism that the other poor sods around me
                    have to cope with, so I avoid it wherever I can.

                    Comment

                    • KH

                      #11
                      Re: Easy Syntax Question

                      > bool var = (a != null ) ? true : false;

                      Note that the ternary operator isn't even necessary there - (a != null)
                      evaluates to a bool anyways.

                      bool var = (a != null); // functionally equivelant without the extra operation



                      "Jonathan Allen" wrote:
                      [color=blue][color=green]
                      > > While it's certainly possible to abuse it[/color]
                      >
                      > My favorite example from real life:
                      > bool var = (a != null ) ? true : false;
                      >[color=green]
                      > > if (foo) {
                      > > s = "foo";
                      > > } else {
                      > > s = "not foo";
                      > > }[/color]
                      >
                      > That can be cut down quite a bit using the If-Then-Else pattern from
                      > FORTRAN/BASIC. Still, not quite as good as it could be.
                      >
                      > if (foo) s = "foo"; else s = "not foo";
                      >
                      > --
                      > Jonathan Allen
                      >
                      >
                      > "C# Learner" <csharp@learner .here> wrote in message
                      > news:14g0xtem8o 5ri.dlg@csharp. learner...[color=green]
                      > > Bruce Wood <brucewood@cana da.com> wrote:
                      > >
                      > > [the ternary operator]
                      > >[color=darkred]
                      > >> Otherwise, it's just a throwback to the days of "look how much code I
                      > >> can stuff onto one line". Ugly and difficult to maintain, IMHO.[/color]
                      > >
                      > > While it's certainly possible to abuse it (as is the case with some other
                      > > language features), there are situations where it helps make the code
                      > > nicer
                      > > and more readable, IMO.
                      > >
                      > > For example, I find
                      > >
                      > > string s = foo ? "foo" : "not foo";
                      > >
                      > > much nicer and more readable than
                      > >
                      > > string s;
                      > > if (foo) {
                      > > s = "foo";
                      > > } else {
                      > > s = "not foo";
                      > > }
                      > >
                      > > Using the 'if .. else' construct here is overkill in comparison, IMO.[/color]
                      >
                      >
                      >[/color]

                      Comment

                      • KH

                        #12
                        Re: Easy Syntax Question

                        My bad -- that <= does check the lower bound.
                        Spoke too soon!


                        "Bill Butler" wrote:
                        [color=blue]
                        > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
                        > message news:uu5lqd6ZFH A.2984@TK2MSFTN GP15.phx.gbl...[color=green]
                        > > Terry,
                        > >
                        > > For the most part, yes. However, to be correct, it should be:
                        > >
                        > > return ((arr == null || arr.Length == 0) ? null : arr[item].ToString());
                        > >
                        > > This assumes that arr might be null.
                        > >[/color]
                        >
                        > I'll Top that :^)
                        > return ((arr == null || arr.Length <= item) ? null : arr[item].ToString());
                        >
                        > That way we don't index outside the Array. (unless you like exceptions)
                        >
                        > Bill
                        >
                        >
                        >[/color]

                        Comment

                        • KH

                          #13
                          Re: Easy Syntax Question

                          You should probably check the lower bound of the array then too -- (item > -1)


                          "Bill Butler" wrote:
                          [color=blue]
                          > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
                          > message news:uu5lqd6ZFH A.2984@TK2MSFTN GP15.phx.gbl...[color=green]
                          > > Terry,
                          > >
                          > > For the most part, yes. However, to be correct, it should be:
                          > >
                          > > return ((arr == null || arr.Length == 0) ? null : arr[item].ToString());
                          > >
                          > > This assumes that arr might be null.
                          > >[/color]
                          >
                          > I'll Top that :^)
                          > return ((arr == null || arr.Length <= item) ? null : arr[item].ToString());
                          >
                          > That way we don't index outside the Array. (unless you like exceptions)
                          >
                          > Bill
                          >
                          >
                          >[/color]

                          Comment

                          • KH

                            #14
                            Re: Easy Syntax Question

                            No it doesn't! You need to check upper and lower bounds --

                            (item > -1 && item < arr.Length)


                            "KH" wrote:
                            [color=blue]
                            > My bad -- that <= does check the lower bound.
                            > Spoke too soon!
                            >
                            >
                            > "Bill Butler" wrote:
                            >[color=green]
                            > > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
                            > > message news:uu5lqd6ZFH A.2984@TK2MSFTN GP15.phx.gbl...[color=darkred]
                            > > > Terry,
                            > > >
                            > > > For the most part, yes. However, to be correct, it should be:
                            > > >
                            > > > return ((arr == null || arr.Length == 0) ? null : arr[item].ToString());
                            > > >
                            > > > This assumes that arr might be null.
                            > > >[/color]
                            > >
                            > > I'll Top that :^)
                            > > return ((arr == null || arr.Length <= item) ? null : arr[item].ToString());
                            > >
                            > > That way we don't index outside the Array. (unless you like exceptions)
                            > >
                            > > Bill
                            > >
                            > >
                            > >[/color][/color]

                            Comment

                            • Bruce Wood

                              #15
                              Re: Easy Syntax Question

                              Yes, I think that was his point.

                              Comment

                              Working...