Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi. I am experimenting with returning an arrays from method and I have question. So lets take a look at the code:
C#
class Program
{
    static void Main()
    {
        string[] strArr = GetNames();
        strArr[0] = "Nik";
        string[] strArr2 = GetNames();
        Console.WriteLine("strArr[0] = {0} and strArr2[0] = {1}", strArr[0], strArr2[0]);
    }

    static string[] GetNames()
    {
        string[] ret = { "Matthew", "Mark", "Luke", "John" };
        return ret;
    }
}


Since its a reference type and when I make my strArr string array and pass to it the returned array from the method GetNames and when I change the first element of the array to another shouldn't it also change the first element in the method's array ?

What I have tried:

Experimented a bit with it and decided to ask here.
Posted
Updated 4-Jul-17 12:27pm
v3
Comments
PIEBALDconsult 4-Jul-17 13:54pm    
No, strings in the CLR have value-type semantics -- they're immutable.

The reason for the difference is that your GetNames method is creating a new array each time it is called. You could rewrite it like this


C#
static string[] GetNames()
        {
            return new [] { "Matthew", "Mark", "Luke", "John" };
            
        }

So strArr and strArr2, in your example, are referencing different arrays. The point raised about immutability of strings is valid but it is not the reason for the behaviour you have illustrated.

 
Share this answer
 
It's complicated. An array is a reference, but it's a reference to an array of references. And strings are a special case, because they are immutable: once created, they cannot be changed, so the following code doesn't necessarily do what you might think:
C#
string x = "XXX";
string y = x;
x = "YYY";
Console.WriteLine("{0}:{1}", x, y);
This will print "YYY:XXX" because the immutability means that the string reference acts like a value - instead of changing the existing reference content, a new reference is created, so the old string is not affected at all.
When you run your code, you get a similar effect:
string[] strArr = GetNames();
returns a reference to an array of strings.
string[] strArr2 = GetNames();
Also returns a reference to an array of strings.
But ... they aren't the same reference, because it isn't the same array!
Try it:
if (strArr == strArr2) Console.WriteLine("Same");
else Console.WriteLine("Different");
Will always print "Different" because despite the strings being the same, the arrays are not.

Every time you execute this code:
string[] ret = { "Matthew", "Mark", "Luke", "John" };
The framework creates a new array, and fills it with the strings for you to return.
If you want to share them, you would need to return the same array:
static string[] ret = { "Matthew", "Mark", "Luke", "John" };
static string[] GetNames()
    {
    return ret;
    }
Now, you will get
C#
strArr[0] = Nik and strArr2[0] = Nik
And "Same" if you do the comparison.
 
Share this answer
 
Comments
The_Unknown_Member 5-Jul-17 9:42am    
Its a little bit confusing
OriginalGriff 5-Jul-17 9:48am    
Probably - but it makes sense when you get your head around it.
What part is confusing?
The_Unknown_Member 5-Jul-17 14:12pm    
This thing:

static string[] ret = { "Matthew", "Mark", "Luke", "John" };
static string[] GetNames()
{
return ret;
}
OriginalGriff 5-Jul-17 14:33pm    
Well, you've used static, so you know what that does.
And the rest is copied from your original code so you know what that does - you wrote it! :laugh:
The_Unknown_Member 7-Jul-17 7:16am    
I wanted to ask why when the array is defined as static and the array is outside the body of GetNames method, why the method is able to return it ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900