17

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?

Neil Knight
  • 44,112
  • 23
  • 121
  • 184

5 Answers5

22

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
  • 5,706
  • 1
  • 24
  • 31
  • 7
    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 Mar 09 '10 at 11:15
  • Doesnt it make a Boxing when you use `string.IsNullOrEmpty` instead of `string.` ? – poudigne Oct 30 '15 at 15:13
6

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
  • 7,134
  • 14
  • 43
  • 77
4

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
  • 8,182
  • 3
  • 32
  • 47
  • 2
    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 Mar 09 '10 at 11:47
2

Personally, I prefered to use CTS types, if I used static methods like IsNullOrEmpty or Parse. But after I read the article Coding Standards for .NET by Lance Hunt (http://se.inf.ethz.ch/old/teaching/ss2007/251-0290-00/project/CSharpCodingStandards.pdf) specially the notice Use built-in C# native data types vs .NET CTS types, I have revised my opinion. Now I use ever corresponding types for declatations and for calling of functions, and I use the aliasses if possible. Consider, maybe are int and Int32 not the same in the future.

Georg
  • 1,540
  • 21
  • 18
0

Since VS2019 suggests to simplify using the struct instead of the class, I would follow its recommendations, even though you must be consistent in your overall solution. Simplification suggestion

Alchoder
  • 21
  • 4