Runtime: Extend the class 'String' with the field 'WhiteSpace'

Created on 30 May 2018  路  9Comments  路  Source: dotnet/runtime

I would be happy if the class System.String had the field WhiteSpace, just like Empty. The field would represent the string with a single white space character.

I've seen may times in a code strings with a single white space character. Typically, when using concatenating strings. I wish instead of " ", there could be more elegant way to do it with string.WhiteSpace.

api-suggestion area-System.Runtime

Most helpful comment

Myself I do not see the value of this. It makes code which seems quite readable, less readable. BTW wouldn't it be called string.Space ?

The compiler already interns string literals so it does not provide that benefit.

All 9 comments

@olegburov so to make sure I understand, instead of eg
```c#
var result = foo + " " + bar + " " + baz;

you would prefer to write 
```c#
var result = foo + string.WhiteSpace + bar + string.WhiteSpace + baz;

Is that right?

If the field is called string.WhiteSpace, it should be allowed to arbitrarily return anything for which string.IsNullOrWhiteSpace returns true, except null.

I wish instead of " ", there could be more elegant way to do it with string.WhiteSpace.

You could just define a proper constant like const string WhiteSpace = " "; and use that.

Personally I find " " more elegant and concise than the proposal, especially with regard to @jnm2's comment.

@danmosemsft yes, you got it right, but I would use the following example to show the benefits of string.WhiteSpace:

```c#
var result = new StringBuilder();

foreach (var item in SomeCollection)
{
result.Append(item.GetSomeResult());
result.Append(string.WhiteSpace);
}
```

@olegburov We could use the same argument to also propose adding String.Comma or String.Semicolon, because generating CSV data is also common.

foreach (var item in SomeCollection)
{
  result.Append(item.GetSomeResult());
  result.Append(string.Comma);
}

Myself I do not see the value of this. It makes code which seems quite readable, less readable. BTW wouldn't it be called string.Space ?

The compiler already interns string literals so it does not provide that benefit.

It's coming up on 2 years and there has been no recent activity on this issue. Is there still interest in this? Otherwise, we should close it.

I don't think this is something we should or would add. I think we can close it. Thanks.

Was this page helpful?
0 / 5 - 0 ratings