xsharp.eu • Best Practices: Space(0) or "" ?
Page 1 of 1

Best Practices: Space(0) or "" ?

Posted: Fri May 21, 2021 2:05 pm
by OhioJoe
When should we use Space(0) to represent an empty string ? Or should we use double quotes ?
Is there a situation where one is preferable over the other?
Is there a different answer for VO and X# ?
And if we're using double quotes, should those be double-double ("") or double-single (' ') ?
I once heard that the difference had something to do with run-time vs. compile-time and there was a speed and memory-overhead advantage to using the Space() function.
We get into these habits over the years and never really think about why we're doing things a certain way.
Thank you, everyone.

Best Practices: Space(0) or "" ?

Posted: Fri May 21, 2021 2:36 pm
by Fabrice
Hi Joe,

in .NET, I have always read it would be better to use the constant String.Empty as it points to the same memory location that contains an empty string : The idea is to avoid to create severals empty string memories, that won't be reused as string are unmutable ( but might be freed by the Garbage Collector )

My 2 cents
Fab

Best Practices: Space(0) or "" ?

Posted: Fri May 21, 2021 3:32 pm
by Chris
Fabrice,

That was true indeed in the first versions of .Net! But nowadays this is not important anymore, all literal strings with the same value now point to the exact same memory location in a special memory reserved by strings, so using "" or String.Empty (which again resolves to "") is the same thing regarding memory. It's a matter of taste now, which version (String.Empty or "") seems more readable to each one.

Joe, using either single or double quotes is absolutely the same thing, if you use the VO dialect, which you do for apps ported from VO. Regarding Space(0), this does have a small overhead, because it is a function call, which then makes another call to create the zero sized screen, so unless you think it's really more readable for you, then better not use it.

I would agree with Fabrice, thet probably String.Empty is the most readable version to use, but that's personal preference of course.