Jaska
Jaska

Reputation: 1430

WCF and ref parameters

I'm having strange issues with WCF and passing parameters as ref.

Let's say I have a Class:

Class MyClass 
{
     public string str;
}

And a code block like this:

List<MyClass> c = new List<MyClass>();
c.Add(new MyClass());
MyClass c2 = c[0];

If I then call a WCF method that should update the str-property of that class:

WCFService.UpdateStr(ref c2);

The c[0] and c2 are different - shouldn't they contain the same string in the str-property!? Is there something wrong in the WCF by ref function parameters?

Upvotes: 1

Views: 1798

Answers (1)

carlosfigueira
carlosfigueira

Reputation: 87238

No, they shouldn't. A parameter passed by reference means that the object itself can be changed, and in the case of WCF calls, it is. When the call to UpdateStr returns, c2 is reference a different object instance.

The image below shows what is going on with this scenario.

enter image description here

Upvotes: 5

Related Questions