Maybe the question sound not good or too simple. But not for me in this case.
My problem is:
I created an extension for string, like
public static void SetString( this string aString, string anotherString ) {
aString = anotherString ;
// process info for that string ... (database, files, etc)
}
If I call that extension like:
string anExistingString = "123";
anExistingString.SetString("Other value");
Console.Write(anExistingString);
but returns 123 instead of Other value…
Where is my mistake ?
This is not possible as an extension method. References to class variables are passed by value, in order to assign a new value to a value passed in a method, you’ll need to use a
refparameter:Personally I consider
refandoutparameters a code smell, it generally implies the method is responsible for more than one thing or is doing something that it shouldn’t. In your example, assignment is far more readable than calling a method.