Best Practices: Space(0) or "" ?

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
OhioJoe
Posts: 131
Joined: Wed Nov 22, 2017 12:51 pm
Location: United States

Best Practices: Space(0) or "" ?

Post 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.
Joe Curran
Ohio USA
User avatar
Fabrice
Posts: 405
Joined: Thu Oct 08, 2015 7:47 am
Location: France

Best Practices: Space(0) or "" ?

Post 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
XSharp Development Team
fabrice(at)xsharp.eu
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Best Practices: Space(0) or "" ?

Post 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.
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
Post Reply