That's because parameters are always passed by value, not reference. And if you think about it, that makes a lot of sense.
If you write a method like this:
private void Double (int x)
{
x = x + x;
}
You could call it like this:
int i = 666;
Double(i);
And you might expect i to be 1332 after that. But if that worked, what would happen if you did this:
Double(666);
Clearly, you don't want to try and change the value of constants!
So when you pass a value - from whatever source - to a method as a parameter, it is copied, and the copy is passed instead: this is called "passing by value" instead of "passing by reference" where the actual item is passed over.
Not you can call Double with a constant or a variable because it doesn't matter - the value outside the method will not be affected by any changes inside it - because it's a copy that is passed, not the "real thing".
You method has the same thing: your string is passed by value into the method - a copy is sent - and any changes are not passed back to the outside world, so the value you give to
str
is discarded when the method exits.
There are two ways to "fix" that: pass it by reference, or return it as a value.
To pass by reference, you do this:
public void AVinfo(ref string str)
And call it like this:
string str = "";
AVinfo(ref str);
And now changes to str inside your method will affect the external variable.
Since you don't use the existing value, you could do that with an out parameter:
public void AVinfo(out string str)
And call it like this:
string str;
AVinfo(out str);
But the better way is to return the value instead of changing it.
public string AVinfo()
{
...
return str;
}
And call it like this:
string str = AVinfo();