String.IsNullOrEmpty or string.IsNullOrEmpty [duplicate]
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?
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.
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..
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.
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.
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.
精彩评论