String.IsNullOrEmpty
-
Hi, Which one gives better performance, checking a string against null & empty. string s = "something";
for(int i=0; i<100; i++) { if(s != null) { if(s.Length > 0) { // do something } } }
orfor(int i=0; i<100; i++) { if(!String.IsNullOrEmpty(s)) { // do something } }
plz help, nas -
Hi, Which one gives better performance, checking a string against null & empty. string s = "something";
for(int i=0; i<100; i++) { if(s != null) { if(s.Length > 0) { // do something } } }
orfor(int i=0; i<100; i++) { if(!String.IsNullOrEmpty(s)) { // do something } }
plz help, nasput it in a loop that will run a few hundred thousand times and use the System.Diagnostics.StopWatch[^]. It's a good idea to get used to testing things like this yourself ... it's quicker than asking on here, more reliable and you have more chance of getting an answer with some of the more obscure stuff (not that IsNullOrEmpty is obsucre).
-
Hi, Which one gives better performance, checking a string against null & empty. string s = "something";
for(int i=0; i<100; i++) { if(s != null) { if(s.Length > 0) { // do something } } }
orfor(int i=0; i<100; i++) { if(!String.IsNullOrEmpty(s)) { // do something } }
plz help, nasOr use Reflector to check what String.isNullOrEmpty does:
public static bool IsNullOrEmpty(string value)
{
if (value != null)
{
return (value.Length == 0);
}
return true;
}IsNullOrEmpty is therefore just a bit slower because there is a strack frame more present (for calling the method). But compared to the time you are using in writing that every time, I'd use IsNullOrEmpty ;)
-^-^-^-^-^- no risk no funk ................... please vote ------>
-
Hi, Which one gives better performance, checking a string against null & empty. string s = "something";
for(int i=0; i<100; i++) { if(s != null) { if(s.Length > 0) { // do something } } }
orfor(int i=0; i<100; i++) { if(!String.IsNullOrEmpty(s)) { // do something } }
plz help, nasSeriously, for the additional fractions of a microsecond that IsNullOrEmpty takes I wouldn't worry about it.
Upcoming FREE developer events: * Glasgow: Agile in the Enterprise Vs. ISVs, db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website
-
Seriously, for the additional fractions of a microsecond that IsNullOrEmpty takes I wouldn't worry about it.
Upcoming FREE developer events: * Glasgow: Agile in the Enterprise Vs. ISVs, db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website
Won't .Length check for null again anyway? If so, you're checking for null twice. I'd just use IsNullOrEmpty figuring Microsoft wouldn't steer me wrong. :-D
-
Won't .Length check for null again anyway? If so, you're checking for null twice. I'd just use IsNullOrEmpty figuring Microsoft wouldn't steer me wrong. :-D
PIEBALDconsult wrote:
Won't .Length check for null again anyway? If so, you're checking for null twice.
.Length
cannot operate on null so it will throw an exception.
Upcoming FREE developer events: * Glasgow: Agile in the Enterprise Vs. ISVs, db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website
-
PIEBALDconsult wrote:
Won't .Length check for null again anyway? If so, you're checking for null twice.
.Length
cannot operate on null so it will throw an exception.
Upcoming FREE developer events: * Glasgow: Agile in the Enterprise Vs. ISVs, db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website
::Smacking forehead with heel of hand::
-
put it in a loop that will run a few hundred thousand times and use the System.Diagnostics.StopWatch[^]. It's a good idea to get used to testing things like this yourself ... it's quicker than asking on here, more reliable and you have more chance of getting an answer with some of the more obscure stuff (not that IsNullOrEmpty is obsucre).