Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: mentalCo. on December 09, 2004, 12:34 PM

Title: [c#]passing by reference
Post by: mentalCo. on December 09, 2004, 12:34 PM
ok heres my problem...

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


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


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



'MyValue' would be printed as 'new value'

how do you do this in c#.

[MyndFyre edit: changed the subject]
Title: Re: [c#]cant think of a good subject name for this
Post by: MyndFyre on December 09, 2004, 03:11 PM
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:


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

void Main(string[] args) {
  num = 5;
  something(ref num);
  Console.WriteLine(num); // outputs 25.
}
Title: Re: [c#]passing by reference
Post by: mentalCo. on December 10, 2004, 01:49 PM
thanks