2

So I'm obviously fairly new to programming, but am trying to figure out why this isn't working

I'm trying to take the string myname, and add Mr. to the start of it. I know I could do it simply as just myname = "Mr. " + myname however I'm trying to understand how to use methods to change the values of variables. So, why doesn't this change?

public class Program
    {
        public static void Main(string[] args)
        {
            string myname = "harry";
            Console.WriteLine(myname); //output is harry

            namechanger(myname); //this should modify harry to Mr. Harry
            Console.WriteLine(myname); //output is still harry?
        }

        static string namechanger(string name) 
        {
            name = "Mr. " + name;
            return name;
        }

    }
Oscar W
  • 464
  • 4
  • 17

1 Answers1

6

Strings are immutable, and passed by value. Every time you create a string, it will never be changed. So unlike instances of classes, you cannot modify a string by handing it to a method which modifies it.

In this case, since you return the modified String in namechanger, all you need to do is make sure you assign myname to the result of that method; like so

myname = namechanger(myname);

Primitive types (int, float, long, etc) work this way, as do struct instances - so be sure to look for that in the future, if you're ever unsure why a struct's value is not changing when you pass it into a method.

Community
  • 1
  • 1
Knetic
  • 2,099
  • 1
  • 20
  • 34
  • 2
    All types work this way, not just primitive types... – aquinas Sep 29 '16 at 02:12
  • Maybe in a pedantic sense, but calling a method with `class` vs `struct` passes wildly different values - one a pointer, one a whole struct. "Pass by value" and "pass by reference" are terms used to distinguish that behavior difference. – Knetic Sep 29 '16 at 02:21
  • @Knetic - No, that's not right. aquinas is correct. – Enigmativity Sep 29 '16 at 02:22
  • @Enigmativity and aquinas ; I'm sorry that you're confused, but this fiddle should help explain to you what these terms mean, and how they apply to the question. https://dotnetfiddle.net/hvxHkr – Knetic Sep 29 '16 at 02:35
  • 1
    The reference to the string is passed by value, really. It's a reference to the same string, not like passing, say, a DateTime, where inside the function it's a different instance of DateTime. – 15ee8f99-57ff-4f92-890c-b56153 Sep 29 '16 at 02:35
  • 2
    @Knetic, that's NOT pass by value and pass by reference. "ref" or "out" is what makes something pass by reference. If you don't include that, it's pass by value. See: https://blogs.msdn.microsoft.com/csharpfaq/2004/03/11/how-are-parameters-passed-in-c-are-they-passed-by-reference-or-by-value/ – aquinas Sep 29 '16 at 02:38
  • Passing a string (passing the value of a reference) vs passing a struct (passing a value): https://dotnetfiddle.net/UuILZj `ref` passes a reference to a reference -- assign a new value to `ref string param` inside the method, and the reference you passed to the method refers now to the new value. – 15ee8f99-57ff-4f92-890c-b56153 Sep 29 '16 at 02:43
  • Huh, I always thought `String` was a `struct`. Either way, both immutability and pass by value are important concepts for OP. I'm glad you contributed. – Knetic Sep 29 '16 at 02:50
  • 1
    @Knetic - I'm not confused at all. There are two competing concepts here - reference/value types versus passing by reference/value. They are two different concepts. The question revolves around passing by value - it has nothing to do with reference/value types. Aquinas was correct in saying that all types work this way. – Enigmativity Sep 29 '16 at 03:49