Ok, you need to go back to basics.
When you declare a class variable, you do not create an instance of that class, you create a variable that can hold a reference to an instance of that class. It's like the difference between a web page and a http: address - the later is a reference to the former, but it isn't the page itself.
When you create two class variables, and assign one to the other, the reference only is copied - not the content - they both refer to the same instance. Looking at in in terms of web pages again, if you have two different sites with an http: link to the same web page, then you would expect any changes to that page to be seen regardless of which site you linked from.
So, when you create a DataSet:
DataSet dataset1 = new DataSet();
DataSet dataset2;
myDataAdapter.Fill(dataset1);
dataset2=dataset1;
Any changes to either dataset1 or dataset2 are reflected in the same instance of a dataset - you are just getting there by different routes.
This is a lot easier to explain with pictures!
"Thanxx
But..String is also a reference type. Why it is not happening for string
string str1="1";
string str2=str1;
str2="2";
//str1 value remains the same, ie "1".."
There are two reasons for this:
1) It does happen exactly the same in the example you give:
string str1="1";
Creates a reference
str1
and assigns a string "1" to it.
string str2=str1;
Copies the reference to the "1" string from
str1
to
str2
. Both variables now refer to the same string: "1"
str2="2";
Creates a new string "2" and assigns the reference to
str2
. This does not change
str1
which remains pointing at the string "1".
I told you this was easier with pictures!
2) Because strings are a special type of object - they are "immutable". What this means is that once a string is created, it cannot be changed. any method which appears to be changing a string content is in fact creating a new string and returning the reference to that - the original string remains untouched.
Don't worry about this yet - it won't affect you for a few weeks when you get a bit more advanced - the first explanation covers your example. I included this for completeness (and because someone would point it out if I didn't and I didn't want to confuse you :laugh:).