473,327 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 ??
Nov 17 '05 #1
16 1340
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.

Nov 17 '05 #2
<"Terry McNamee" <n/a>> wrote:
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 ??


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
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]
- mv*@spam.guard.caspershouse.com

"Terry McNamee" <n/a> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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 ??

Nov 17 '05 #4
Thanks Guys. :-)
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uu**************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Terry McNamee" <n/a> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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 ??


Nov 17 '05 #5
Bruce Wood <br*******@canada.com> wrote:

[the ternary operator]
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.


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.
Nov 17 '05 #6
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uu**************@TK2MSFTNGP15.phx.gbl...
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.


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
Nov 17 '05 #7
> While it's certainly possible to abuse it

My favorite example from real life:
bool var = (a != null ) ? true : false;
if (foo) {
s = "foo";
} else {
s = "not foo";
}
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" <cs****@learner.here> wrote in message
news:14***************@csharp.learner... Bruce Wood <br*******@canada.com> wrote:

[the ternary operator]
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.


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.

Nov 17 '05 #8
Jonathan Allen <x@x.x> wrote:
if (foo) s = "foo"; else s = "not foo";


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.
Nov 17 '05 #9
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.

Nov 17 '05 #10
KH
> 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:
While it's certainly possible to abuse it


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


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" <cs****@learner.here> wrote in message
news:14***************@csharp.learner...
Bruce Wood <br*******@canada.com> wrote:

[the ternary operator]
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.


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.


Nov 17 '05 #11
KH
My bad -- that <= does check the lower bound.
Spoke too soon!
"Bill Butler" wrote:
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uu**************@TK2MSFTNGP15.phx.gbl...
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.


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

Nov 17 '05 #12
KH
You should probably check the lower bound of the array then too -- (item > -1)
"Bill Butler" wrote:
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uu**************@TK2MSFTNGP15.phx.gbl...
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.


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

Nov 17 '05 #13
KH
No it doesn't! You need to check upper and lower bounds --

(item > -1 && item < arr.Length)
"KH" wrote:
My bad -- that <= does check the lower bound.
Spoke too soon!
"Bill Butler" wrote:
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uu**************@TK2MSFTNGP15.phx.gbl...
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.


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

Nov 17 '05 #14
Yes, I think that was his point.

Nov 17 '05 #15
Bruce Wood <br*******@canada.com> wrote:

<snip>
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.


My problem with that approach is - how far do you take it? Do you
assume that they don't know that && and || are shortcircuiting, too? Do
you assume they don't know default access modifiers? Do you assume they
don't know default variable values? What about the using/lock
statements, which will confuse C/C++ programmers if they don't know C#?
I think sooner or later you have to assume that your co-workers have a
working knowledge of the language.

My view is that if something is somewhat onerous to remember (like
exact precedence rules) then it helps to make it more explicit. If it's
easy to foul up even with experience (like using single statement "if"s
with no braces) then it helps to make it more explicit.

If it's a simple language construct which is easy to explain once and
then ask people to remember, I don't see what's wrong with it. Of
course, you have to also have an environment where people are happy to
ask if they don't understand something, and have enough people with
good knowledge (or the ability to find something in a language spec) to
help them if they get stuck.

As a side issue, what does the VB code in your work environment look
like? There's so much *language* to learn in VB in the first place, if
everything has to be understandable to everyone, I don't see how it can
work...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #16
Good points, all. I guess this is just baggage from other programming
jobs, back when I was programming in C and everything was relatively
straightforward. Back then I avoided the ternary operator, assignments
inside conditions, and other unnecessary C-isms out of respect for the
other programmers around me.

Now that, as you point out, there are far bigger fish to fry in C#, my
prejudice against the ternary operator is probably outdated. After all
those years trying to write clearer code, though, it just looks ugly to
me, so I guess it's devolved into an aesthetics thing. :-)

Nov 17 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
33
by: Jim Hill | last post by:
I've done some Googling around on this and it seems like creating a here document is a bit tricky with Python. Trivial via triple-quoted strings if there's no need for variable interpolation but...
3
by: Iain | last post by:
I am attempting to run the folliwing update: update rx set startdate = date_sub(startdate, INTERVAL 14 DAY) where rowid in ( select rx.rowid from rx, episode where rx.rxbatch=0 and...
8
by: Perre Van Wilrijk | last post by:
Hello, I have 2 ways of updating data I'm using often 1) via a cursor on TABLE1 update fields in TABLE2 2) via an some of variables ... SELECT @var1=FLD1, @var2=FLD2 FROM TABLE1 WHERE...
9
by: Mark Walsh | last post by:
The following is an explanation of a bug that exists in the VB.NET compiler. I've tried to report it to Microsoft, but they've made it so difficult I've given up: MSDN help states that variables...
1
by: Ryan Smith | last post by:
I can't get past this - I am inserting a record into a db and I keep getting the same SQL SYNTAX ERROR MESSAGE. Below is the statement i am using with test data. All fields in the database are...
2
by: Michael | last post by:
Hi, why is it ok to have: function home_mouseover(){ document.getElementById("hom").src = home_night.src; } <a href="./home.shtml" onmouseover="home_mouseover();">
7
by: SteveM | last post by:
I am sure this is an easy question, but being relatively new to ASP.NET programming, I can not quite grasp what I need to accomplish what I need to do. What I have is a word document that is...
20
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.