Using string.Empty
instead of ""
is really cute and make the code more clear.
I'm wondering if there's a good named constant to replace " "
too.
I've found some ideas like using string.Empty.PadLeft(1)
or string.Empty.PadRight(1)
, but I don't like it.
Something like string.Space
to use instead of " "
would be appropriate for the situation.
(Edited after comments)
To make my question's situation more clear:
In the multicultural situations, there shouldn't be any code like "Can not open the file"
. The string literals should be moved to a resource file and then use like Resources.CanNotOpenTheFile
.
To make sure that happens, It seems a good rule, not to have any string literals in the code. So looking code at a glance, you can find bad implementations quickly. I think that's a good explanation of why I'm trying not to use "
in the code.
A string constant is an arbitrary sequence of characters that are enclosed in single quotation marks (' '). For example, 'This is a string'. You can embed single quotation marks in strings by typing two adjacent single quotation marks.
String constants, also known as string literals, are a special type of constants which store fixed sequences of characters.
In C#, Replace() method is a string method. This method is used to replace all the specified Unicode characters or specified string from the current string object and returns a new modified string. This method can be overloaded by passing arguments to it.
That is not possible. In . NET strings are immutable, which means that you can't change a string. Save this answer.
There is none, but you can define your own.
public static class MyString
{
public const string Space = " ";
}
Then use it like:
Console.Write("Test" + MyString.Space + "Text");
EDIT But you shouldn't, IMO (and based on comments from Marc Gravell and PaulRuane) since that will make the code less readable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With