bhavu

Hi,

string is a reference type, still while passing in function it doesn't behave like that

I have below function

private void button1_Click(object sender, System.EventArgs e)

{

string s = "";

test(s);

Messagebox.show(s);

}

private void test(tring str)

{

str = "this";

}

When I run above program, it shows me empty string. it should show "this" ................

why it's like this




Re: Visual C# General string is a reference type, still while passing in function it doesn't behave like that

Matthew Watson

Because strings are designed to be immutable. You should treat them as a value type.

See the Microsoft MSDN documentation for System.String for details.






Re: Visual C# General string is a reference type, still while passing in function it doesn't behave like that

TilakGopi

Hi,

yes string is reference type only.It means,that u can have another string variable as reference type.What ever the modifications done to that reference ,get reflected inthe original one.

To achieve this , u need to create a reference for the former string,and initialize the later with the former.Now it will work.

In functions ,this is achieved by using 'ref' key word before parameter,in function signature as well as in function call.

To achieve this,

u have to modify ur code as follows



private void button1_Click_1(object sender, EventArgs e)
{
string s = "";
test(
ref s);
MessageBox.Show(s);
}
private void test(ref string str)
{
str =
"this";
}

Did i reach u

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# General string is a reference type, still while passing in function it doesn't behave like that

Younes Ouhbi

the System.String type is a reference type, but string are also immutable, which means that once a string has a value, you can't change it unless another string is created in memory.

That's one reason for the recommended use of StringBuilder instead of string concatenation that if done extensively could slow down performance...





Re: Visual C# General string is a reference type, still while passing in function it doesn't behave like that

Matthew Watson

@T.Gopi:

That's somewhat misleading. Although String is ostensibly a reference type, it has been implemented to be immutable, so that it acts as if it were a value type.

If a string behaved as a reference type, it would NOT be necessary to use the 'ref' keyword in order to change it within a method.

When you use the 'ref' keyword, the reference itself is changed, not the referand. It's the same as changing a string pointer to point to a different string, which is NOT the same as changing the original string.

For example, here's some code where a reference type actually behaves as a reference type. You can change the data inside the object inside a method, without using the "ref" keyword:

The reference class:



public class Demo
{
public int Data;
}



And the code:


private void button1Click(object sender, EventArgs e)
{
Demo demo = new Demo();
demo.Data = 1;
Test(demo);
MessageBox.Show(demo.Data.ToString()); // Shows "2" not "1".
}

private void Test(Demo demo)
{
demo.Data = 2;
}







Re: Visual C# General string is a reference type, still while passing in function it doesn't behave like that

TilakGopi

Hi,

u need to excuse my ignorance.

Thanx & Best Regards,

Ch.T.Gopi Kumar.






Re: Visual C# General string is a reference type, still while passing in function it doesn't behave like that

Ernst Kuschke

Guys, seriously... forget about the whole "string is immutable" argument. Yes, string is immutable if you look at the unmanaged internals thereof, but it is abstracted for coders so that it doesn't make a difference whether it's immutable or not.

Also, don't care too much whether it's a value type or a reference type in this case. The behavior for value- and reference types would be the same here! Also this is abstracted from coders.

To do what you intended to do, you need the "ref" keyword:

private void button1_Click(object sender, System.EventArgs e)

{

string s = "";

test(ref s);

Messagebox.show(s);

}

private void test(ref string str)

{

str = "this";

}


Now you will see "this" in your messagebox Wink






Re: Visual C# General string is a reference type, still while passing in function it doesn't behave like that

Matthew Watson


Yes, string is immutable if you look at the unmanaged internals thereof


Or if you read the Microsoft documentation about the string type!

You must be aware that it is immutable. Most programmers coming from C++ start doing things like calling "s.ToLower()" expecting it to convert 's' to a lowercase string. Of course, it does not.

If you are made aware that strings are immutable before you start using them, you will be unlikely to make such mistakes!

Therefore I must respectfully - but strongly - disagree with your statement that "
it doesn't make a difference whether it's immutable or not".

In addition, if you are aware that strings are immutable, then you will also likely be aware that they are much more threadsafe as a consequence.





Re: Visual C# General string is a reference type, still while passing in function it doesn't behave like that

Chris Dunaway

Matthew Watson wrote:
For example, here's some code where a reference type actually behaves as a reference type. You can change the data inside the object inside a method, without using the "ref" keyword:

The reference class:



public class Demo
{
public int Data;
}



And the code:



private void button1Click(object sender, EventArgs e)
{
Demo demo = new Demo();
demo.Data = 1;
Test(demo);
MessageBox.Show(demo.Data.ToString()); // Shows "2" not "1".
}

private void Test(Demo demo)
{
demo.Data = 2;
}


Just to add, in this instance the reference is passed by value so if you tried code like this:



private void Test(Demo demo)
{
demo = new Demo(); //does not change the original demo reference
demo.Data = 2; //only changes the local demo object and not the original
}

Then the original demo object would not be affected since, when passing a reference type by value, you cannot change the reference. If the ref keyword were used then you could change the reference.

Chris





Re: Visual C# General string is a reference type, still while passing in function it doesn't behave like that

Ernst Kuschke

Dude, I know it is immutable. I wrote an article about this at the time I learned it - [1].
However, that *only* affects you if you write *unmanaged* code! People get confused about this - as long as you write managed code, it doesn't affect you at all

My point is that the guy who asked the question didn't have any unmanaged (unsafe) code, so it wouldn't affect him at all. (Most people completely misunderstand the fact that it's immutable, as can be seen by some replies to this post).

1 - http://dotnet.org.za/ernst/articles/1153.aspx





Re: Visual C# General string is a reference type, still while passing in function it doesn't behave like that

Matthew Watson

Ernst Kuschke wrote:

However, that *only* affects you if you write *unmanaged* code! People get confused about this - as long as you write managed code, it doesn't affect you at all


Sorry to labour the point, but you seem to be misunderstanding me.

Anyway - my point was that the fact that strings are immutable DOES affect you. I gave two examples why:

(1) Where I work, many people changed to C# from C++. Many of these made the mistake of calling string.ToLower() expecting the string to become lowercased like it does with MFC's CString. But because string is designed to be immutable, you must use the return value from string.ToLower() instead. This caused several bugs!

If it had been made clear to the programmer that string was immutable, they would have been far less likely to make a mistake.

(2) Because a string is immutable, you can more safely use them in multithreaded situations.

In my humble opinion, a programmer really should be aware of the fact that strings are immutable.

While I'm here, let's have a look at the code that you presented earlier:



private void button1_Click(object sender, System.EventArgs e)
{
string s = "";
test(ref s);
Messagebox.show(s);
}

private void test(ref string str)
{
str = "this";
}



I think it may be better to treat the string as a value type, and code it like so:



private void button1_Click(object sender, System.EventArgs e)
{
string s = "";
s = test(s);
Messagebox.show(s);
}

private string test(string str)
{
// Do something with str
return "this";
}



I think this is more in keeping with the way string methods themselves work. Would you agree

(By the way, I'm sorry that you seem to be getting upset that I disagreed with you...)




Re: Visual C# General string is a reference type, still while passing in function it doesn't behave like that

Ernst Kuschke

Hi!

Firstly, sorry if I sounded upset - I think I just needed a cup of coffee at the time I posted Wink

I agree 100% with most of what you write, especially that developers *should* be aware that strings are immutable. However, I think that your example for .ToLower() still does not relate to strings being immutable Smile This is an example of where I think the "immutable-ness" of strings confuses people.

string a = "STRING";
string b = a.ToLower();

The fact that the method returns a *new* string and points b at this instance is not because a string is immutable - they might as well have (behind the scenes) done it like so:

string a = "STRING";
a.ToLower();

and have variable "a" now point at a *new* instance that contains "STRING" - nothing changes in string being immutable! (The original value still didn't change, and still sits in memory).

The way this method works is be design, and unaffected by a string being immutable. A string being immutable is, from a managed perspective, a pure implementation detail, and should not bother us at all.





Re: Visual C# General string is a reference type, still while passing in function it doesn't behave like that

Matthew Watson

Ok! Fair enough. Smile