Home / Articles / Programming Languages / C# / C# File Input Output III / Attributes

“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.

C# Programming Series

C# File Input Output III

Yashavant Kanetkar
Yashavant Kanetkar


Attributes

Attributes add extra information to a class, a method, data members, properties or any element to which they are attached. The element to which we can attach an attribute is called the target of the attribute. Attributes are attached using brackets and are written just before declaring the target. For example, to mark any data member as non-serializable in the above program we would use the [NonSerialized] attribute.

In our program, suppose we add a static data member called count to the shapes class to keep a count of shapes generated. We would increment this data member in the constructor. Since there is no need to serialize count we can declare it as non-serializable by attaching an attribute [NonSerialized] with it. This ensures that when objects of the shapes class are written in a file, all the data members of every object except count would get written in a file. This is how the class declaration would look like:

 [Serializable]
 abstract class shapes
 {
        protected int r, g, b;
        [NonSerialized]
        static int count;
        public shapes()
        {
            Random rd = new Random();
            r = rd.Next(255);
            g = rd.Next(255);
            b = rd.Next(255);
            count++;
            Thread.Sleep(5);
        }
        public abstract void draw(Graphics g);
    }

Also, if we want to mark an int variable as dead or not in use, we have to mark it with an Obsolete attribute:

[ Obsolete ]
int i ;

This marks i as obsolete and any use of i will result in an error. This example is not very practical but it helps to understand what attributes are and how to attach an attribute to some element.

Attributes are aliases for their corresponding classes defining them. For example, Obsolete is an alias for the System.ObsoleteAttribute class. All the functionality of the attribute is written in the class. Some attributes accept parameters that are supplied inside pair of parenthesis immediately after the name of the attribute. In the same way we have many predefined attributes in .NET programming. .NET also allows us to write our own custom attributes.

Comments

Log in or create a user account to comment.

On Sale From April 2008

Let Us C
8th Ed.
C programming classic & best seller. 1 million+ copies sold!

Y. Kanetkar

On Sale From April 2008

Introduction to Object Oriented Programming & C++

Y. Kanetkar

On Sale From Fall 2008

Microsoft .NET Framework: Web Application Security

Vijay Mukhi

On Sale From Nolvember 2008

Quest C++ Courseware
12+ hours of instructional audio and animated slides.

Y. Kanetkar Asang Dani

On Sale From November 2008

A Programmer's Guide to Web Application Security

Vijay Mukhi

Latest Forum Posts