Reputation: 6093
hi i am little bit confused in valu type or referance type...
for instance int is a value type string is referance type but int also referances some value
like int int i=4
int j=i
here j is also referanced 4 as like i so why not we call it as a referance type......in case of string s="hello"
string s1=s
here s is referancing s.....will any one explain in storage point of view ......is there any restriction over valu type or referance type.
Upvotes: 0
Views: 320
Reputation: 9617
A value type holds a value, a reference type holds a reference to another variable.
In your example, i holds the value 4, but j=i means j still holds the value 4, even if i changes to 5. j doesn't change it's value, so it's not a reference type.
Strings are the same way. You can say s1 = s, but when s changes to 'hello2,' s1 stays 'hello'. Acts like a value type, but under the hood, stored like a reference type, see below.
Arrays and objects are reference types. If you say obj2 = obj, and obj changes, obj2 also changes. Obj2 holds a reference, or pointer to, obj, changes and all.
also see: Visual Basic .NET Syntax Reference Primer
containing:
A string is initialized as follows:
Dim myString as String = "Hello, World!"
Strings are Immutable, meaning once you assign a value to them they cannot be changed. Whenever you assign another value to a string or concatenate a value to a string, you are actually creating a new copy of a string variable and deleting the old copy of the string. (This has potential performance considerations if you are doing repeated work on a string variable, use the StringBuilder for such tasks as it is not immutable ie. The StringBuilder is Mutable).
I think it's more accurate to say you're deleting the old reference to the string variable, as any other strings pointing to it retain their value.
Upvotes: 1
Reputation: 55947
Ask yourself what happens when things change:
int i = 4;
int j = i;
i = 7; // Q1). now what value does j have?
j = 99; // Q2) what value does i now have?
Q1). assigning 7 to i has no effect on j, j still has the value 4.
Q2). assigning 99 to j has no effect on i, i still has the (latest) value 7;
Strings are a bit tricker to explain, because in some languages you can't change them. So I'll use an (invented) language where it's legal to change values in a string.
string s1 = "Hello";
si[0] = 'J'; // now s1 is set to "Jello"
So we say
string s1 = "wibble";
string s2 = s1;
// here s2 is **referring** to "wibble", the same "wibble" that s1 refers to.
s1[1] = 'o';
// now what does s1 refer to? "wobble"
// Big question: what does s2 refer to? The very same thing, "wobble".
So s1 and s2 are references to the same thing at the moment. We could now write
s1 = "SomethingElse";
Now we have chnaged the reference in s1, but s2 still refers to "wobble"
Upvotes: 1
Reputation: 60095
int i=4;
int j=i;
int
is value type, it means that this expression j=i
copies value from i to j.
string s="hello";
string s1=s;
string
is reference type so here not value is copied but reference to that string, so they now point to same object.
Upvotes: 1