Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
class str
    {
        string s1 = "111";
        int[] ar1 = new int[1];

            public void add()
        {
         //   string s2 = s1;
            int[] ar2 = new int[1];
            ar2 = ar1;
            ar1[0] = 4;

            Console.WriteLine(ar1[0]);
            Console.WriteLine(ar2[0]);
            //s2 = "222";
                ar2[0] = 5;
            Console.WriteLine(ar1[0]);
            Console.WriteLine(ar2[0]);
        }
    }




in this example if we are giving the array value to second array,it is getting updated in first array also.Because it is reference type.But if we are doing in string,it is not working ....Why??
Posted

Because strings are immutable i.e they are read-only and their values cannot be modified. Every time you do an operation on a string, a new String Object is created.

For more information check the MSDN link - String Class[^] and scroll down to immutability.

Read the article and you will have a very clear picture about String objects.
 
Share this answer
 
Comments
VJ Reddy 9-Apr-12 9:41am    
To the point. +5
Ankur\m/ 9-Apr-12 9:55am    
Thank you for your vote. Much appreciated!
Monjurul Habib 10-Apr-12 1:15am    
5!
String in .NET is a reference type but it is an immutable type, meaning that it can't be changed once it is created.

string s = string.Empty;
s = "Hello, World";
s = "Hello, " + "World";


In this case three new string objects will be created for s.

http://msdn.microsoft.com/en-us/library/362314fe.aspx[^]
 
Share this answer
 
Comments
VJ Reddy 9-Apr-12 9:40am    
To the point. +5
Monjurul Habib 10-Apr-12 1:15am    
5!
It's not simple to explain, but I'll try:
The first thing you need to know is that strings are immutable - when you declare a string (say "hello there") it cannot be changed, not even slightly. When you try, (Say by adding " Samu!") what happens is that the system adds the lengths of the two strings, allocates enough memory for the result, and copies both of then into it, returning the new string value - the original string is not changed in any way.
The other thing is that when you declare an array:
C#
int[] ar1 = new int[1];

You have done two operations:
1) You have decalared a variable called ar1 which can reference an array of ints.
2) You have constructed a new array of integers and assigned it's reference to ar1

What that means is that when you subsequently declare another variable:
C#
int[] ar2 = new int[1];
ar2 = ar1;
You first assign a reference to a new array to it, then overwrite that reference with the one that ar1 is referring to. ar1 and ar2 now both reference that same array in memory.

So when you change the array content via ar1 it affects the memory that ar2 is also refering to.

When you do "the same thing" with strings, it all happens the same:
C#
string s1 = "111";
Declares a string variable s1, and assigns it a reference to the memory holding "111".
C#
string s2 = s1;
Declares a string variable s2, and copies the reference from s1. The two variables now refer to the same string in memory: "111"
C#
s2 = "222";
Changes the reference of s2 to a new string in memory. It does not affect s1 in any way - s1 continues to refer to the memory holding "111"
 
Share this answer
 
v2
Comments
VJ Reddy 9-Apr-12 9:40am    
Nice explanation. +5
samu4u 9-Apr-12 10:01am    
so it will be just like an value type??
even 'int' is also doing the same thing,??
why they dint made string in that way??
so reference wont work over there... perfectly??right>??
thanks for the great explanation...
OriginalGriff 9-Apr-12 10:05am    
No, a value type does not need the reference - it goes directly.
This is complex without pictures...
Do you understand what a pointer is?
samu4u 9-Apr-12 10:11am    
it goes directly means to an address location, so that we never want to bother
about the location..
and int allocating memory directly..and the variable directly holding data rather than pointer type or reference type.
pointer concept is clear for me...
OriginalGriff 9-Apr-12 10:57am    
Good!
A Reference is effectively a pointer: so when you declare a variable of a reference type, you are in fact declaring a pointer to memory-which-will-hold-the-data. When you declare a variable of a value type, you are declaring the actual memory which holds the value.
int is a value type, an array (of any form) is a reference type. So when you declare
int i = 6;
int[] a = new int[1];
you declare a value type called "i", which contains "6" and a reference type called "a" which holds a pointer to an array of value types.

If you re-read my explanation above, substituting "pointer" for "reference" you will hopefully get the idea!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900