473,545 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1351
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.co m>
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.co m

"Terry McNamee" <n/a> wrote in message
news:%2******** ********@tk2msf tngp13.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.c om> wrote in
message news:uu******** ******@TK2MSFTN GP15.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.co m

"Terry McNamee" <n/a> wrote in message
news:%2******** ********@tk2msf tngp13.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*******@cana da.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.c om> wrote in
message news:uu******** ******@TK2MSFTN GP15.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*******@cana da.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

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

Similar topics

699
33349
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it...
33
9123
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 requiring a long, long formatted arglist via (%s,%s,%s,ad infinitum) if there is. So my question is: Is there a way to produce a very long...
3
1308
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 rx.handwritten=0 and episode.patient=rx.patient and episode.doctor=1029
8
3172
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 FLD-ID = @inputVAR UPDATE TABLE2 SET FLDx = @var1, FLDy = @var2
9
1299
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 are initialised when they are created. This seems to be true for the following code which produces "1","2","3","4","5". (ie the variable is created...
1
1064
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 text fields with no restrictions. Syntax error in INSERT INTO statement. INSERT INTO...
2
1131
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
2435
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 rendered as a page (or actualy a part of a page) that is editiable. To do this I let the user download the document, edit it and then upload it back to...
20
2872
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 through some LINQ queries. But how to cast? ( See the below, modified from somebody else's code. The problem is the query 'stops' (throws a cast...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.