tags:

views:

64

answers:

6

hi

I have one doubt in string . How can we acess the memory of string?

is String is reference type or Value type?

1) if it is Reference type then do 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 value of string is not changed. My question is why the string value is not changed? If it is reference type then the string value should 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 value is changed because emp is reference type

2) if string is value type

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

then why do they mentione that string is a class?

A: 

The String type is a reference type, but it is immutable. This means you can't change the contents of the string.

It actually behaves like a value type, but only references are passed around, instead of the whole string object.

Philippe Leybaert
+1  A: 

This is happening because of this part:

s = "wser";

This is exactly equivalent to:

s = new String ("wser");

When you write this, a new object is instantiated and its reference is stored in s. Thus, the previous reference is completely lost in the function scope and no change is noticed outside the scope.

Thus, to notice changes in a reference type changed in another scope, you cannot create a new instance and assign it to the variable name, you must modify the original reference itself (and this is not possible for a string object in Java - strings are immutable).

Crimson
Notice this is C#, not Java... :)
Noldorin
My bad :) - but I am leaving the answer around because the concept applies.
Crimson
A: 

From the MSDN online page - string

Strings are immutable--the contents of a string object cannot be changed. Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references.

http://stackoverflow.com/questions/93091/why-cant-string-be-not-immutable-in-java-and-net

adatapost
+1  A: 

If you store the reference to an object in a variable and then change the reference, the object does not change, only the variable. You're changing a propert of em, thus affecting the object references by the variable em. If you instead did em = something, it would behave like the String example and not affect anything either.

sepp2k
+2  A: 

The System.String type is indeed a reference type, albeit a rather strange one. It is immutable from a developer's perspective, and the CLR treats it pretty much like a value type, so you won't go far wrong doing the same.

Jon Skeet's article on parameter passing, value types, and reference types in C#, also gives a good explanation of this curiosity:

Note that many types (such as string) appear in some ways to be value types, but in fact are reference types. These are known as immutable types. This means that once an instance has been constructed, it can't be changed. This allows a reference type to act similarly to a value type in some ways - in particular, if you hold a reference to an immutable object, you can feel comfortable in returning it from a method or passing it to another method, safe in the knowledge that it won't be changed behind your back. This is why, for instance, the string.Replace doesn't change the string it is called on, but returns a new instance with the new string data in - if the original string were changed, any other variables holding a reference to the string would see the change, which is very rarely what is desired.

Noldorin
A: 

String is a reference type.

The reason why the string in the list is not changing in your example, is that the method doesn't get access to the item in the list, it only gets a copy of the reference. The argument s is just a local variable in the method, so assigning a new value to it doesn't affect the contents of the list.

Guffa