23

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?

0

5 Answers 5

27

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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().
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..

Comments

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.

1 Comment

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.
3

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.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.