Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
When this will play a key role? Do we Really need string.IsNullOrEmpty ?


C#
objClaimListApproval_SC.Reim_Token_ID =
         string.IsNullOrEmpty(txt_Token_ID.Text) ? string.Empty : txt_Token_ID.Text;
Posted
Updated 5-Aug-13 5:28am
v3
Comments
Sergey Alexandrovich Kryukov 5-Aug-13 11:30am    
Key role or not key role... this is a really absent-minded kind of question. Look at the API, see what it does and decide by yourself what to do with it. The question really lacks the subject.
—SA
anurag19289 5-Aug-13 13:03pm    
ok.. thanks..

String.IsNullOrEmtpy just returns a true/false value depending on if the string passed in actually has any content. That expression just checks for the existence of a string value and returns the value. If there is no value (null) or the passed in string is equivilent to "" or String.Empty, it returns String.Empty.

The code you supplied is a single line version of:
C#
string result;
if (string.InNullOrEmpty(txt_Token_ID.Text))
{
    result = string.Empty;
}
else
{
    result = txt_Token_ID.Text;
}


Frankly, this code is absolutely useless as TextBox.Text will NEVER return a null value. It's always going to be a string of 0 (String.Empty) or more characters.
 
Share this answer
 
Comments
Joezer BH 5-Aug-13 11:27am    
5ed!
Sergey Alexandrovich Kryukov 5-Aug-13 11:39am    
5ed.
—SA
ridoy 5-Aug-13 12:11pm    
my 5!
It's handy.
If you have a method which accepts a string:
C#
private void MyMethod(string s)
    {
    ...
    }
Then you can't afford to just use the string directly:
C#
if (s.Trim() == "")
    {
    ...
    }
Because the attempt to call Trim on a null value will cause a "Null Reference" exception.
Coding it as:
C#
if (string.IsNullOrEmpty(s))
    {
    ...
    }
Avoids that, and does the same check to ensure the string isn't just "" at the same time.
In .NET 4.0 and above, you also get:
C#
if (string.IsNullOrWhitespace(s))
    {
    //...
    }
Which is even more helpful!
 
Share this answer
 
Comments
Joezer BH 5-Aug-13 11:26am    
5ed!
Sergey Alexandrovich Kryukov 5-Aug-13 11:38am    
5ed.
—SA
anurag19289 5-Aug-13 11:39am    
So you mean to say like :

if string s is null


and s.trim will give nullreference exception...


so i can avoid it by writing like this

if (string.IsNullOrEmpty(s))
{
s= string.empty;
}
else
{
s= s.trim();
}
OriginalGriff 5-Aug-13 11:43am    
Yes - but I'd use IsNullOrWhiteSpace if I could:
if (string.IsNullOrWhiteSpace(s)) s = "";
which covers null, "", and " " all in one.
Of course it is useful, it is mostly useful to avoid the double check!
IsNullOrEmpty is a convenience method that enables you to simultaneously test whether a String is null or its value is Empty. It is equivalent to the following code:
C#
result = s == null || s == String.Empty;
One possible usage is your line of code in the question, an other can be to avoid the following (unfortunately common) mistake:
C#
string CheckString(string str)
{
   if (str == null) 
      return "Empty!";
   else 
      return "Not Empty";
}


Cheers,
Edo
 
Share this answer
 
Comments
anurag19289 5-Aug-13 11:34am    
but can textbox.text return null ever ?
I thing if the text box is empty then it will always be--> "" [which is nothing but string.empty]

So is the code below is useful ? correct me if i am wrong...


string.IsNullOrEmpty(txt_Token_ID.Text) ? string.Empty : txt_Token_ID.Text;
Joezer BH 5-Aug-13 11:36am    
No, the textbox.Text property will never return null.
so actually in this case it's not a very usage of the IsNullOrEmpty method...

Your question was however about the "key role" of the method and how to use it.
Sergey Alexandrovich Kryukov 5-Aug-13 11:38am    
Correct.
—SA
Sergey Alexandrovich Kryukov 5-Aug-13 11:37am    
5d. I think however that the OP's problem is methodological. I tried to say something in my comment to the question. That's why the real answer (if you emphasize the point) is "no": the method is not essential because of the existence of equivalent code you provided. Note the importance of '||' (using '|' would be a failure). This is no more but convenience.

This is actually related to ongoing argument between some developers, what to use to conduct "no string" notion: null, empty or both, which I think is simply counter-productive, especially from the site of those who think that null and empty should be unified. The method IsNullOrEmpty is in a way a work around to cover such dirty unification.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900