Search code examples
c#datacontractserializer

Why do some DataMember attributes have brackets while others have not?


The MSDN documentation about the DataMemberAttribute class shows a example below.

Sometimes brackets are set and sometimes are not. What is the reason behind?

Example:

[DataMember()]
    public string FirstName;

 [DataMember]
    public string LastName
    {
        get { return LastNameValue; }
        set { LastNameValue = value; }
    }

Solution

  • This is a C# syntax issue. It has nothing to do with these particular classes.

    Brackets are optional for attributes if you do not pass arguments to the constructor of the attribute.

    I recommend that you decide which style you like and stick to it.