We help IT Professionals succeed at work.

What do the brackets mean in c#? I mean these" [ ] when they are not referencing arrays

freezegravity
on
Medium Priority
546 Views
Last Modified: 2012-05-05
I am trying to read some C# code that someone else has written.

I am coming across this line and am having trouble understanding it.

        [System.Xml.Serialization.XmlIgnore()]

in

    public class Schedule
    {
        public struct Interval
        {
            public int length;
            public string unit;
        }
        [System.Xml.Serialization.XmlIgnore()]
        public bool intervalSpecified;
        public Interval interval;
}

I am weak at Serialization, but that is another issue. I was thinking that [] was only an array symbol, but System.Xml.Serialization.XmlIgnore doesn't seem to be part of any array. If I remove the [], then I get a "Invalid Token" error. So, essentially, what does the [] do?

Thanks!
Comment
Watch Question

look at this article
http://msdn2.microsoft.com/en-us/library/a3hd7ste(vs.80).aspx

the square brackets seem to be referring to a "pointer" or a token to something else in the application.
Top Expert 2007

Commented:
Statements like that above methods, properties or members are called Attributes. In this case, it acts like qualifier, and will cause a serializer to ignore the boolean member called intervalSpecified. You can also define and create your own custom attributes that can be very useful when using reflection into your own assemblies.

Jim
That is called an Attribute, that one is specifically an XML Serialization Attribute. Those Markers are used at specify certain things. That attribute in particular is used to tell a the program at runtime Not To Include That Data Member In Serialization.
Liquid90605:Perfect answer go with it.

Author

Commented:
After reading the answer, I still had a few questions. However, I looked them up and the answer is correct.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.