Valhalla Legends Forums Archive | .NET Platform | [c#]passing by reference

AuthorMessageTime
mentalCo.
ok heres my problem...

In c++ you can do something like this...

[code]
void something(char *crap){
memcpy(crap, "new value", 9);
}


void main(){
char MyValue[100];
something(MyValue);
cout<<MyValue;
}

[/code]

'MyValue' would be printed as 'new value'

how do you do this in c#.

[MyndFyre edit: changed the subject]
December 9, 2004, 6:34 PM
Myndfyr
That's somewhat odd code for C#; you wouldn't copy string data.  You wouldn't want to anyway; .NET maintains a string table.

But, you can pass value-types by reference:

[code]
void something(ref int num) {
  num *= num; // squares it.
}

void Main(string[] args) {
  num = 5;
  something(ref num);
  Console.WriteLine(num); // outputs 25.
}
[/code]
December 9, 2004, 9:11 PM
mentalCo.
thanks
December 10, 2004, 7:49 PM

Search