3

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?

luxun
  • 457
  • 5
  • 14
  • 1
    If you remove Order, it will use a default order (alphabetical). In your example, the explicit order is already alphabetical so it won't matter, but if your explicit Order were not alphabetical, then removing the Order attribute would change the serialization order and break existing clients. Often Order is used to group members that correspond to different versions of a DataContract. – Joe Aug 31 '17 at 09:44
  • I think that's the answer I was looking for @Joe. Is grouping together members that correspond to different versions a standard method for handling different versions of a data contracts? – luxun Aug 31 '17 at 09:54
  • 1
    It's mentioned in Microsoft best practice documents: see 8c in the section "Versioning When Schema Validation Is Not Required" of https://learn.microsoft.com/en-us/dotnet/framework/wcf/best-practices-data-contract-versioning – Joe Aug 31 '17 at 10:00

2 Answers2

1

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

Ankit
  • 5,733
  • 2
  • 22
  • 23
0

Where I work, we have a C# API that interfaces with a third-party system that uses an unusual method of database interaction in which field order is very important. When serializing an object that represents a record from a table in that system into a format that said system can understand, it is useful to us to have this order represented in the C#.

In short, it can be useful if order matters to whatever 'thing' is consuming the serialized data from the object.

Zoe
  • 27,060
  • 21
  • 118
  • 148
ornsio
  • 61
  • 5