I thought string was a reference type?!

wyrd

Senior Contributor
Joined
Aug 23, 2002
Messages
1,405
Location
California
C#:
using System;

namespace ConsoleApplication3
{
	class Class1
	{
		[STAThread]
		static void Main(string[] args)
		{
			new Class1();
		}

		public Class1() {
			string stuff = "yayaya";
			Console.WriteLine("before: " + stuff);
			changeString(stuff);
			Console.WriteLine("after: " + stuff);
			Console.Read();
		}

		private void changeString(string str) {
			str = "changed!";
			Console.WriteLine("function changed: " + str);
		}
	}
}

This prints out the following;

before: yayaya
function changed: changed!
after: yayaya

Am I missing something?! I thought string was an object (reference type) while basic types like int, double, short, etc. and also enums and structures were value types.

:confused:
 
Strings are indeed objects but they are immutable. Anything you do where it looks like you're doing something to a string, you're actually creating a new copy of it.

If you modified your above code to pass the string by reference instead of by value, you would get the result expected.
 
Still that doesn't make sense to me. It should be automatically passed by reference, as it's an object. It's nothing more then a pointer to the first char in the char array which makes up the "string", and assigning it to a new "string" would do nothing more then point it to a new location.

*mumbles*
 
I just read up on reference types and passing parameters in the MSDN library. It makes sense now according to the way .NET works. I guess I should get C++ off the brain.

Oh, and I learned about a new keyword I didn't know about before; out

:D
 
:rolleyes:

Yes, I know everything in .NET is an object. But I think you knew what I ment when I was talking about the string object (which is a reference type, not a value type)

My point was, passing reference types acts a tad differently then what I was thinking for some reason.
 
wyrd said:
...But I think you knew what I ment when I was talking about the string object (which is a reference type, not a value type)...
No, I didn't. Sorry if it seemed that way. I just thought I would point it out in case you (or someone searching the forum in the future) didn't know.
 
In case anyone is wondering, this is the correct way to get the above code working. Just need to assign to variable:

C#:
using System;

namespace ConsoleApplication3
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            new Class1();
        }

        public Class1() {
            string stuff = "yayaya";
            string newstuff = changeString(stuff);
            Console.WriteLine("before: " + stuff);
            Console.WriteLine("after: " + newstuff);
            Console.Read();
        }

        private string changeString(string str) {
            str = "changed!";
            Console.WriteLine("function changed: " + str);
            return str;
        }
    }
}

I only posted this for any n00bs out there that were curious as to how to make the above code function the way Wyrd seems to have expected it to.

--Sean
 
Back
Top