tags:

views:

208

answers:

3

I have been looking through code for the last 3 days, and the original developer is defining Strings using the String class rather than the string class. So, when they've used the IsNullOrEmpty method, it's defined String.IsNullOrEmpty.

What I'd like to know is how is the compiler dealing with String.IsNullOrEmpty compared to string.IsNullOrEmpty?

Is there any advantages using String.IsNullOrEmpty over string.IsNullOrEmpty?

+10  A: 

They are both the same.

string is a keyword alias in c# for System.String.

Only difference is that when using String, you need to use either System.String.IsNullOrEmpty or using System; at the begining of your code file.

logicnp
Thanks for the quick response.
Ardman
Create yourself a static class called StringUtilities and define an extension method called IsNullOrEmpty:public static bool IsNullOrEmpty(this string @this) { return string.IsNullOrEmpty(@this); }.This allows you to call it as a method of the variable you are checking, e.g. fred.IsNullOrEmpty().
Paul Ruane
A: 

They are the same.

Personally, I prefer to use String.IsNullOrEmpty. The alternative just doesn't look right. The same goes for choosing Int32.Parse(...) over int.Parse(...). And, of course, no matter what approach you choose, be consistent.

Rune
I see, so it's more from a readability point of view?
Ardman
Yeah. string.IsNullOrEmpty(...) looks like we're trying to do a method call on a keyword (depending on your syntax highlighting settings of course). But we're really splitting hairs here. I keep to String.IsNullOrEmpty, but I don't fix it if I see string.IsNullOrEmpty in my colleagues' code.
Rune
+1  A: 

String stands for System.String and it is a .NET Framework type. string is an alias in the C# language for System.String. Both of them are compiled to System.String in IL (Intermediate Language), so there is no difference. Choose what you like and use that. If you code in C#, I'd prefer string as it's a C# type alias and well-know by C# programmers.

I can say the same about (int, System.Int32) etc..

šljaker