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 "" ?
Best Practices: Space(0) or "" ?
Joe Curran
Ohio USA
Ohio USA
Best Practices: Space(0) or "" ?
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
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
XSharp Development Team
fabrice(at)xsharp.eu
fabrice(at)xsharp.eu
Best Practices: Space(0) or "" ?
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.
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.
Chris Pyrgas
XSharp Development Team
chris(at)xsharp.eu
XSharp Development Team
chris(at)xsharp.eu