I read a programming handbok and they recomment to use String.Empty instead of "" . I can't see why

T

Tony Johansson

9. Use String.Empty instead of ""
Good:
If ( name == String.Empty )
{
// do something
}
Not Good:
If ( name == "" )
{
// do something
}

//tony
 
R

Registered User

9. Use String.Empty instead of ""
Good:
If ( name == String.Empty )
{
// do something
}
Not Good:
If ( name == "" )
{
// do something
}

The use of String.Empty clearly indicates the author's intention of using an
empty string. Double quotation marks could mean an empty string or that the
author meant to add the proper string value later and failed to do so.

regards
A.G.
 
M

Marcel Müller

9. Use String.Empty instead of ""
Good:
If ( name == String.Empty )
{
// do something
}
Not Good:
If ( name == "" )
{
// do something
}

They may write whatever they want but I find the latter more natural to
read.


Marcel
 
A

Arne Vajhøj

9. Use String.Empty instead of ""
Good:
If ( name == String.Empty )
{
// do something
}
Not Good:
If ( name == "" )
{
// do something
}

I think you need the answer to two questions.

1) Should MS had created the String.Empty constant?

Answer: no.

It is best practice to replace magic literal values in code
with named constants.

For two reasons:
* it ensures a consistent change when the values has to be changed
* the constant name can provide more information about what the values
is than then just the value

But neither applies here:
* the value of String.Empty will never change from ""
* the name String.Empty is not providing more information
than ""

Purely aesthetic reasons are not sufficient to warrant creation
of a constant.

So please - never create a class:

public class FooBar
{
private const int ZERO = 0;
private const int ONE = 1;
...
}

But to some extent that does not really matter.

MS did create String.Empty and C# developers has started
using it.

So new question:

2) Since MS has created String.Empty should we use it?

Answer: it does not matter, but the same convention should
be used throughout the entire solution.

All developers will understand both String.Empty and "".

But the code is easier to read if a consistent style is used.

So decide on a style and stick to it.

Arne
 
M

Mathias

There is also a technical difference,

String.Empty is on one memory address and all references to it points to the same address.

Each "" allocates a new memory adress. So, it you use the constant the application will use (slightly) less memory. This is true for all constants used more than once.

/Mathias

Den onsdagen den 6:e november 2013 kl. 04:04:31 UTC+1 skrev Arne Vajhøj:
 
M

Martin Shobe

There is also a technical difference,

String.Empty is on one memory address and all references to it points to the same address.

Each "" allocates a new memory adress. So, it you use the constant the application will use (slightly) less memory. This is true for all constants used more than once.

/Mathias
[snip]

I just ran a test in VS 2010, and even in debug mode there was only one
address used for all of the ""s in the assembly.

Martin Shobe
 
M

Mathias

Cool!
It's probably the compiler that automatically replaces empty string literals with string.Empty. What about two equal nonempty string constants?

/Mathias

Den fredagen den 15:e november 2013 kl. 15:06:45 UTC+1 skrev Martin Shobe:
There is also a technical difference,

String.Empty is on one memory address and all references to it points to the same address.

Each "" allocates a new memory adress. So, it you use the constant the application will use (slightly) less memory. This is true for all constants used more than once.

[snip]



I just ran a test in VS 2010, and even in debug mode there was only one

address used for all of the ""s in the assembly.



Martin Shobe
 
M

Martin Shobe

Cool!
It's probably the compiler that automatically replaces empty string literals with string.Empty. What about two equal nonempty string constants?

/Mathias
[snip]

string.Empty resulted in a different address than "". But it was able to
use the same address for multiple instances of nonempty string constants.

Martin Shobe
 
A

Arne Vajhøj

There is also a technical difference,

String.Empty is on one memory address and all references to it points to the same address.

Each "" allocates a new memory adress. So, it you use the constant the application will use (slightly) less memory. This is true for all constants used more than once.

So you think that the MS documentation for System.String is wrong then?

I mean it does state:

<quote>
The common language runtime conserves string storage by maintaining a
table, called the intern pool, that contains a single reference to each
unique literal string declared or created programmatically in your
program. Consequently, an instance of a literal string with a particular
value only exists once in the system.
</quote>

!!

Arne
 

Ask a Question

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.

Ask a Question

Top