tags:

views:

48

answers:

4

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.

+1  A: 

You should reed this article: Value vs Reference

Vash
+1  A: 
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.

Andrey
+1  A: 

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.

Beth
Strings are immutable but very much a reference type, they merely behave like value types for equality comparison purposes.
Anthony Pegram
I think that's what gets people confused. Under the hood, strings are reference types until they're changed, when they get a new reference to the new value. The other strings referring to the original value don't follow along to the new one.
Beth
But think about it, string assignment is no different than other reference type assignments. `string a = "abc"; string b = a; a = "123";` is not different than `Foo a = new Foo("abc"); Foo b = a; a = new Foo("123");` In both situations, the object referenced by `b` has not changed. The fact that strings are immutable is what throws people off, as string *manipulation* always results in a new string. But changing the string entirely with the `=` operator is not unique to `string` versus other reference types.
Anthony Pegram
To be honest, I'm not sure why he's asking, or what understanding he's trying to gain. Strings are stored as references, but from an assignment point of view, act like value types. Understanding string behavior is simple enough, understanding memory allocation is different. I don't know what kind of 'restrictions' over types he's asking about, but that might help us answer his question.
Beth
+1  A: 

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"

djna
again i am confused from your last statement while when we chance s1="somethingelse" then why not s2 will change bcz you s2 and s1 is referance type
Nishant
hello sir i am confused from your last statement.....will you elaborate it plz sir..
Nishant
nice sir now i got it actually in last statement you are changing refrance it self which is pointed by s1..but here interasting point is that s2 still refer s1 previous value. ..........thank's a lot
Nishant
Yep, that's the key thing: it's a reference, so what is it actually referring to now.
djna