String variable value changed

I have one doubt in the line. How can we save string memory?

Is this a link type or a value type?

1) if this is a link type, execute the following code

    List<String> strLst = new List<string>() { "abc","def","12"};
    strLst.ForEach(delegate(string s)
    {
        if (s == "12")
        {
            s = "wser";
            // I doubted whether = operator is overloaded in string class 
            //StringBuilder sb = new StringBuilder(s);
            //s = sb.Append("eiru").ToString();
            s = String.Concat(s, "sdf");
        }
    });

See that the string value does not change. My question is why the string value does not change? If it is a reference type, the value of the string must be changed.

class emp
{
    public string id;
}


List<emp> e = new List<emp>() { new emp() {  id = "sdf" }, new emp() { id = "1" }, new emp() { id = "2" } };

e.ForEach(delegate(emp em)
{
    if (em.id == "1")
        em.id = "fghe";
});

Here the value changes because it empis a reference type

2) if string- value type

public sealed class String : IComparable, ICloneable, IConvertible, IComparable<string>,     IEnumerable<char>, IEnumerable, IEquatable<string>

then why do they mention that the string is a class?

+3
source share
6 answers

This is due to this part:

s = "wser";

This is exactly equivalent to:

s = new String ("wser");

, s. , , .

, , , , ( ) Java - ).

+2

System.String , . , CLR , , .

, # :

, (, ) - , . . , . - , , , , , . , , string.Replace string, , - , , , .

+2

, , . em, em. em = something, .

+1

String , . , .

, , .

0

- MSDN -

- . , (== ! =) , .

Java .NET?

0

.

, , , , . s - , .

0

Source: https://habr.com/ru/post/1717051/


All Articles