Examples to understand string immutablility in c# [duplicate] - c#

This question already has answers here:
Why can't strings be mutable in Java and .NET?
(17 answers)
Closed 8 years ago.
What is meant by "strings are immutable in c#". I need some examples to understand this.I can not find some proper examples to understand this

This means that if you assign
string s = "Hello";
you cannot modify the string s. Thus, if you do
s = "Goodbye";
the literal "Hello" is not modified, but a new literal "Goodbye" is assigned to s.

Searching on your text "strings are immutable in c#" I find: http://msdn.microsoft.com/en-us/library/362314fe.aspx
Which seems to be saying that strings are never changed objects, they are always destroyed and re-created.
Per Microsoft:
Strings are immutable--the contents of a string object cannot be changed after the
object is created, although the syntax makes it appear as if you can do this.
For example, when you write this code, the compiler actually creates a new string object
to hold the new sequence of characters, and that new object is assigned to b. The string "h"
is then eligible for garbage collection.
string b = "h";
b += "ello";

Related

Find String Array in other String Array [duplicate]

This question already has answers here:
Finding a subsequence in longer sequence
(8 answers)
Closed 1 year ago.
I want find a string array in other string array. How I do that?
Example:
string[] a = {"a","b","c","d","e","f","g","c","d","e"}
string[] b = {"d","e"}
How find b in a, I need get the index of all instances.
I would suggest you search a bit of the documentation to understand more of the language but anyway I'll try to explain what you can do
You can do a for loop going through the a array and inside you do a for loop going through the b array, if a[index] = b[index2] you break out of the a loop and put the index in a new array that you initialized before
If you need more help than that say but I recommend going through more documentation and a few videos and learn a bit more the basics

What is ".#checked" meaning and called? [duplicate]

This question already has answers here:
What's the use/meaning of the # character in variable names in C#?
(9 answers)
Closed 5 years ago.
When I read a source code C#, I see a line
return new Form(ElementType.Checkbox, ((IHTMLInputElement)htmlElement).#checked);
But I can NOT find it with keyword .#checked.
Could you please tell me what is .#checked called and its meaning?
Update answer
Firstly, Thanks #Yeldar Kurmangaliyev, #Erik Funkenbusch and #TcKs.
Secondly, I'm sorry for making duplicate post. Because I searched with ".#checked" and saw nothing.
Finally, I understood that # sign help define a variable whose name is as same as C# keyword, like this:
string #string = "This is a string";
string str = #string; // str = "This is a string"
The checked is keyword in C# and therefore the name must be prefixed with # as documentation says:
Keywords are predefined, reserved identifiers that have special
meanings to the compiler. They cannot be used as identifiers in your
program unless they include # as a prefix. For example, #if is a valid
identifier but if is not because if is a keyword.
So ((IHTMLInputElement)htmlElement).#checked) means accessing the checked member on instance of class implementing IHTMLInputElement.

How many string additions do you need for a StringBuilder to be more efficient? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Stringbuilder vs String.Concat
As I understand it, String are immutable, so if you do "one " + "two", rather than reusing the memory of these two strings, a new string is created with value "one two". This becomes a problem if you do something like:
String output = "";
for (int i = 0; i < 20; i++){
output = output + ii.toString();
}
Because 19 (or 39?) strings are created and discarded to produce the final desired output.
String builders are mutable, so you can add strings together efficiently, and then convert them into a string when you are done.
I assume there is some overhead in creating string builders, and also the clever compilers used today optimize away lots of string addition problems. So my question is: how many strings do I need to add together before it becomes more efficient to use a StringBuilder instead? Or are my assumptions incorrect?
An answer in this question - Does StringBuilder use more memory than String concatenation? - states 'a few dozen' is the threshold.
And linked from another answer in that question is this link - http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html - which concludes with "it doesn't matter"!

string is a Class-why [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C#, why is String a reference type that behaves like a value type?
Why in C# string is a class/ref type , where as int/double are value/struct- any specific reason or it is by design
Integral types have the important property of being accessible as a whole by the processor in one go. It is not the case for a string which may be composed of thousands of bytes, so in all languages, strings have always been pointed to, because the computer cannot really do it any other way.
In an object language like C#, it is canonical to create a class to point to a memory location: that's actually what an object is about.
So yes, strings are classes, because they can't be integral types.

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