3

i want to know what is the difference between them and what is the posiible way to use them


5 답변


21

Absolutely no difference. Not even at the IL level. In C# string is an alias to the actual System.String .NET type. As to the possible use, well, use it whenever you want to represent an empty string in your application. There have been many discussions as to what is the difference between string.Empty and "" and the general consensus is that you should use the one that suits you best and makes your code more readable which obviously is subjective.


  • var s2 = String.Empty requires a import statement for "System". var s2 = string.Empty does not. Don't know why im mentioning it, =) - RPM1984
  • Oh and +1 for a good answer on a bad question. - RPM1984
  • i hv seen ppl using string name= string.Empty and protected string _PK_ID=String.Empty why they are using this..... just for making code readable........... - user
  • @RPM1984, string is alias to System.String. That's why the import is required. Still no difference at the emitted IL by the compiler. - Darin Dimitrov
  • It used to be that using a reference to String.Empty was better than having many empty strings using "". Every time you created an empty string with "", it would allocate memory for the string, and it's already been allocated for String.Empty. These days however, the C# compiler automatically performs string interning, so it's not so bad. I still prefer the use of a named constant over a magic string though - so I always go for String.Empty. - Tim Roberts

2

No difference. string is alias of System.String


  • string is an alias for System.String in mscorlib. You can define your System.String in your assembly, but string will not change. - gandjustas

1

No difference, it’s just predefine text or name for compiler.

string =String (class)
int =Int32 (struct)
long= Int64 (struct)
decimal = Decimal (struct)

Compiler interpret as String class and others as relevant structure.


0

Just FUI: There is one difference between aliases - Int32 and int, for example:

You could write:

enum A : int
{
}

But you can't write:

enum A : Int32
{
}


-1

As far as I know, there is no difference. Atleast, no difference in the outcome.

You may use it like this:

string mystring = "blah";

mystring = string.Empty;

Or

String mystring = "blah";

mystring = String.Empty;


Linked


Related

Latest