|
I'd agree with Lopati - IsNullOrEmpty is a specific question (as is IsNullOrWhitespace) which specifies exactly what conditions it checks for: HasValue doesn't, it implies a degree of "valueness" which is not implicit to a string, but is added by context. HasValue could mean "has a valid numeric", "has a name and address" which "IsNullOrEmpty" doesn't - it's a basic generic check which prepares the way for more explicit "value" checking.
I'll stick with IsNullOrWhitespace myself!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: I'll stick with IsNullOrWhitespace myself! Only because you can, it doesn't mean you should
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
You can use extension methods for all sorts of test (e.g. comparisons) to handle null arguments, so you don't have to explicitly test for null (or cause an exception when a null occurs).
This is obviously a good idea only if a null value is meaningful in the context. E.g. rather than
if (middleName != null && middleName.Equals("Peter")) ...
I write
if (middleName.Eq("Peter")) ...
with the Eq extension method checking the this argument for null before using it in an Equals test.
|
|
|
|
|
Another option would be to use the null conditional operator:
if (name?.Length >= 3) But the null conditional operator will only test for null, not for empty ...
modified 19-Feb-19 7:15am.
|
|
|
|
|
RickZeeland wrote: if (name?.Length >= 3) However, this will return 'false when the input is a null string, or an empty string, or any string with 'Length < #3.
So, why do you say it tests only for 'null ?
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Updated the answer
|
|
|
|
|
RickZeeland wrote: Updated the answer i know my eyesight is in deep trouble, but I can't see what changed
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
There's nothing negative about it, despite your "!".
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
A better name for the function is string.IsNotNullAndHasALengthGreaterThanZero - there, very positive and precise!
- I would love to change the world, but they won’t give me the source code.
|
|
|
|
|
Using your logic, that function name should be:
string.IsNotNullAndNotEmptyAndHasALengthGreaterThan2
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
You're right! Although perhaps it should be:
string.IsNotNullCommaNotEmptyAndHasALengthGreaterThanZero
...I'm not sure where the Length > 2 part you mentioned came in.
- I would love to change the world, but they won’t give me the source code.
|
|
|
|
|
I think it mostly depends on how the string identifies itself at any given moment - this is for all the snowflakes out ther (you know who you are).
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
How to swear in Latin[^]
What do you get when you cross a joke with a rhetorical question?
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
Do questions with multiple question marks annoy you???
|
|
|
|
|
So useful!
I was still calling people coprolite. I guess this is much more advanced!
|
|
|
|
|
This would be much more interesting if it included a list of curses used by the Romans, rather than translations of modern curses.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Even in Latin, not exactly KSS or appropriate for the lounge, IMO.
Latest Article - Slack-Chatting with you rPi
Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny
Artificial intelligence is the only remedy for natural stupidity. - CDP1802
|
|
|
|
|
"Leno = pimp"
Is that what he's up to these days...
|
|
|
|
|
|
As bad as cats, sometimes...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
They are little deities and therefore can't fail at anything, so it must be your fault.
Did you ever see them get angry after doing something clumsy and then being laughed at? There are not so many animals that would be able to understand the meaning of laughter at all, much less the concept of Schadenfreude.
I have lived with several Zen masters - all of them were cats.
His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.
|
|
|
|
|
|
Had to do my Roomba away; it stole food from the cat, and they couldn't be friends anymore.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I'm stealing one of the comments from the page since the language used suits my mood currently.
Quote: Of all the things that didn't happen this didn't happen the most.
Pick a 'reality-show' - any of the bloody things. They're a more honest representation of what actually happened than this. The final direction that end of the roll of paper-towel points in dispels any remaining uncertainty.
Fun video though, but the Roomba didn't do this alone.
|
|
|
|
|
37. Blessing contains small particle (5)
|
|
|
|
|
Blessing BO ON
contains
small S
particle
BOSON
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|