S
Sami
string = "" or string = string.Empty?
should is the proper way?
Sami
should is the proper way?
Sami
string = "" or string = string.Empty?
should is the proper way?
Sami
string str = string.Empty is the proper and more efficient way.
Thanks,
Well, it's not more efficient as they are both string literals, but the
code more closely resembles the intention.
If you always use string.Empty when you want an empty string, you know
that when you find "" in your code there is something that you
accidentally removed or something that you forgot to put there.
[...]
If you always use string.Empty when you want an empty string, you know
that when you find "" in your code there is something that you
accidentally removed or something that you forgot to put there.
I've been told that string.Empty does not require any user memory, and
every time you reference "" you use a little bit more user memory to
store the literal.
You've been told wrong then. At worst, all uses of the explicit literal
"" will use the same object, due to string pooling (I think in C# they
call it interning...same thing).
Joe said:You've been told wrong then. At worst, all uses of the explicit literal[...]
If you always use string.Empty when you want an empty string, you know
that when you find "" in your code there is something that you
accidentally removed or something that you forgot to put there.
I've been told that string.Empty does not require any user memory, and
every time you reference "" you use a little bit more user memory to
store the literal.
"" will use the same object, due to string pooling (I think in C# they
call it interning...same thing).
Evenif the references are in difference classes in the same project?
Göran Andersson said:Well, it's not more efficient as they are both string literals, but the
code more closely resembles the intention.
If you always use string.Empty when you want an empty string, you know
that when you find "" in your code there is something that you
accidentally removed or something that you forgot to put there.
The classic reason for using names instead of values is
that if you want to change the value, then you only need
to change it one place.
Very good reason. But it does not apply here. You will
never want to change the value of the empty string.
And "" is actually easier to read, so I will consider that
the proper way.
You could forget to put something within the "" but you could
also forget to write something instead of String.Empty - I can
not see the difference.
Arne
And if FxCop is to be believed this is actually the most efficient way --Jonathan said:Although I prefer the latter of the two, I don't think it makes much
difference.
Personally, I would use string.Length == 0.
Code_Breaker said:A short note on efficiency can be found here.
http://www.csharp411.com/stringisnullorempty-shootout/
Yes, we're going off on a tangent. But for assigning, I'd like to think thePeter said:But since the original question was about _assigning_ strings, not
comparing them, whether FxCop is correct or not doesn't matter. It has
no bearing on what the "preferred" way to _assign_ an empty string to a
variable would be.
I do agree with everything else you wrote. Surely the question of
comparing empty strings is significant only in a particularly unusual
program. But that's not what the OP was asking about.![]()
"" will use the same object, due to string pooling (I think in C# they
call it interning...same thing).
I personally like to use "", because sometimes you can mistake
string.Empty for being null.
It's essentially the same thing, although there might be some
difference under the hood.
But I believe that string.Empty just does = "", so it's just one more
level of redirection (although I have to check up on the value of
string.Empty)
If you have a doubt about what something does in the .NET Framework,
just google for DotNetReflector and download it. It's a great program
that uses reflection to disassemble the IL code of any .NET assembly
(including mscorlib and the like) and then convert it to C# or some
other languages (although some code is obfuscated).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.