Why is string.Empty readonly? [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why isn't String.Empty a constant?
...and not const?
I know its probably a useless question, but I'm sort of curious on the reasoning behind this one. An empty string is an empty string so I do not foresee many chances of string.Empty being anything else than "". So why make it readonly?
Is there any other benefit I'm missing in not making it const?

It IS static.
public static readonly string Empty;

Readonly prevents it from being changed. Perhaps you mean "why isn't it a const"?

Related

Modifying static variables from other scripts in Unity [duplicate]

This question already has answers here:
What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?
(12 answers)
Closed 7 years ago.
Does anybody know how to modify a static variable from a different script in Unity?
Please, provide more information about how are you trying to do this...
If you need to change between mono behaviours, for example, you will need to get current instance of behaviour you like to change in a way like
myGameObjectInstance.GetComponent<MyBehaviourWithStatic>().myStatic = value;
but note that user2320445 answer is not wrong, it depends on your context. Be more specific on your question and we could provide better and precise answers

Is there a specific reason that String.Empty is not a Const? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why isn't String.Empty a constant?
I wanted to use string.Empty in the Attributes of a few of my properties, but have since seen that it is not actually a Const but a static member.
Is there any reason Microsoft would do this?
I would say it's always a pretty bad idea using const in referenced assemblies.
The reason being the fact that the C# compiler treats constants as values and not as references, as I've said in this answer.
With this I mean that the C# compiler will replace all instances of the constant in you code and replace the "variable" with the value.
This means that even if you update the assembly GlobalConstants.dll and copy it to one of the applications you have, you will need to recompile that application. Not doing so, will cause the application to use the old constant values.
To overcome this problem, you can simply use public static readonly instead of public const as the readonly modifier differs from the const in that it is treated by the C# compiler as a reference in code rather than a value.
I think that the reason is: string is reference type, not value type. and it is faster to compare two references (when you use static member) than two instances of strings (when you use const)

Is that correct using of "this" keyword? [duplicate]

This question already has answers here:
When do you use the "this" keyword? [closed]
(31 answers)
In C#, is "this" keyword required? [duplicate]
(6 answers)
Closed 9 years ago.
I would like to know if I get it correctly: also with this keyword I can distinct between fields and variables?
Like this:
class X
{
int x;
public X(int x)
{
this.x=x;
}
}
Yes, if a method parameter (or local variable) has the same name as a field, you need to use this to distinguish the two. Also, StyleCop is very vocal about every class member access being done through this, but whether that's a good idea or not may be up to debate. It makes things more clear, but also adds much visual clutter.

C# String.Empty [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the difference between String.Empty and “”
Hello
Simple Question;
Why
Textbox1.Text = String.Empty;
is better than
Textbox1.Text = "";
?
It's not, really.
Basically, decide which you find to be more readable. Personally I use "" instead of string.Empty, but others prefer the latter.
Back in .NET 1.x days apparently there was some tiny performance difference (almost certainly irrelevant in real apps) but I believe these days even that's gone.
Use whichever you and your find most readable.
Because String.Empty is actually defined as ""
Best practices dictate not to use string literals but constants.
On a funnier note: String.Empty is/looks more Object Oriented-ISH.

Difference between string.Empty and "" [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C#, should I use string.Empty or String.Empty or “” ?
Is there any difference in c# between the following declarations...
private string m_port = string.Empty;
or...
private string m_port = "";
Is it just coding standard that makes it look neater?
Just coding standard...
http://msdn.microsoft.com/en-US/library/system.string.empty%28VS.80%29.aspx
They are the same, but looks better.
The main difference is one of semantics - string.Emtpy says that you meant to have an empty string. "" might be a mistake (" ", for instance).
Since String.Empty is an instance it should remove the overhead of the object creation, however I think a parser should be smart enough to figure this out.
All string literals is also pulled from an object pool I believe, so the object creation might not even take place.

Categories