I've been working with some serializable classes recently and they typically look something like this:
[DataContract]
public class Foo
{
[DataMember(Order = 0)]
public string Bar1
{
get;
set;
}
[DataMember(Order = 1)]
public string Bar2
{
get;
set;
}
}
I was wondering, what possible application could one have for specifying an order for the data members? The guidelines here specify that it may "Sometimes it may be necessary to change this order." but don't give any examples of when or why. Do you have any examples of when or why this may be necessary?
In my application I am simply serializing these objects of these types and saving them down to a file. Does specifying an "Order" here have any value or does it simply add something else to maintain? Would I face any problems if I simply removed "Order" from each data member?
Order
specifies the order in which data members are serialized into an object, otherwise they'll be arranged alphabetically.
In some applications, it is useful to know the order in which data from the various data members is sent or is expected to be received (such as the order in which data appears in the serialized XML).
One of the scenarios where you would like to order your data members when you are sending response to a client with pre-defined object structure.
Refer this question for example.
Also sometimes you might be interested in showing the ID field (or similar) first, and then all the other fields, to make raw xml look end-user friendly.
And yes you won't get any error if you don't mention order.
Refer docs here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With