views:

39

answers:

1

As many of you will be aware, I can use the DataContract attribute to mark a class for serialization into XML in a WCF service.

For example, I can create a DataContract thus:

< DataContract() > Public Class fooClass
< Datamember() > Public fooString as String
End Class

When I add a service reference to the code that will receive this DataContract, I see that the class generated by the designer has fooString as a public property with a backing field. My question is, why does the designer use a backing field? I don't see any reason not to access fooString directly.

+2  A: 

It is code design "standard" style used by MS. And is for a good reason to use properties instead of public fields. Even if on server side you will have data contract like this :

[DataContract]
class MyComplexType
{
   [DataMember]
   public int id;
   [DataMember]
   public string name;  
} 

In client proxy class you will have them as backing fields named something like idField, nameField...

So it is not connected to WCF itself it is matter of why to use properties instead of public fields for which you can easily find guidance.

Incognito