Predefined attributes in c#

Sourav.Kayal
Posted by in C# category on for Intermediate level | Points: 250 | Views : 8058 red flag

This article is to show how to use predefined attribute in C#.

Introduction

In this article I am going to discuss few important attributes of C# . There are two types of attribute in C#

1)      User defined attributes

2)      Pre defined attributes

In this article I am going to discuss few very important pre defined attributes. Rather than discuss theory of attributes I will be showing example of them followed by simple introduction of attributes.

What is attributes? 

Let me give informal definition of attribute (Please search in google for bookish definition). Attributes are nothing but some extra information injected with your normal c# code. For example I have written some statement in my code and I want to inform C# compiler how to precede that particular code. So according to attribute value compiler will take decision.

Another more example, I have change something in some existing function and though I have changed it I don’t want to remove it. Now if anyone try to call my old function I will inform then “Hey..One new version has added for same function ..Why not you try it?”. So using attribute we can perform such kind of magic.

Let’s start with example

According to my word , I will show example of few important attributes. Microsoft has given lot of very essential attributes to work with c#. Some of them are

  1. STAThread
  2. Obsolete
  3. Serializable
  4. Conditional
  5. NonSerialized
  6. ThreadStatic

So, In our example we will see one by one with example

STAThread

If we use STAThread attribute in our program, It represents that our program will follow Single thread model in threading concept of C#

The implementation of this attribute is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test1
{
    class Program
    {
        [STAThread]  //Single thread model
        static void Main(string[] args)
        {
        }
    }
}
 

MTAThread

MTAThread stands for multi thread model. If we use MTAThread we ensure that our program will adapt multi threading concept in runtime. The implementation is like below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test1
{
    class Program
    {
        [MTAThread]  //Multi thread model
        static void Main(string[] args)
        {
        }
    }
}   

Obsolete

This is very useful attribute given by .NET library. If we want to obsolete any function or code snippet then we can use obsolete attribute in above. In my program ,it takes two argument the first one is string message and second one Boolean value . This Boolean value determine whether the message will display as warning or Error.

True-> Error

False-> Warning

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test1
{
    class Test
    {
        [Obsolete("Please use new function",true)]
        public void OldFunction()
        {
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            t.OldFunction();
        }
    }
}

Serializable

Serializable attribute is used to ensure that the particular code segment will be Serializable during serialization. We can use this attribute in front of class/ method / property of class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test1
{
    [Serializable]
    class Test
    {
        public void OldFunction()
        {
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
   
        }
    }
}
  
 

NonSerialized

This is exact opposite of Serialized attribute. We can use this attribute anywhere in class(like Serialized) but it ensure that the particular code will not maintain it’s state during Serialization.

using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Test1
{
    class Test
    {
        [NonSerialized]
        public String Name;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.ReadLine();
        }
    }
}
 
 

Conditional

If you start your programming life with C programming then you might aware of conditional compilation feature. Conditional attribute is very similar with it. If we define attribute value in program then the code snippet(here Msg() function] will compile otherwise not. Sample code is in below.

#define EXECUTE_ME
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Test1
{
    class Test
    {
        [Conditional("EXECUTE_ME")]
        public static void Msg()
        {
            Console.WriteLine("Hello");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Test.Msg();
            Console.ReadLine();
        }
    }
}
 

ThreadStatic

Threasstatic attribute is only applicable for static member. It may be static data member or function member. If we use Threadstatic then it ensures that the static field will create for each and every thread separately. Below is sample example of Threadstatic attribute.

using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Test1
{
    class Test
    {
        [ThreadStatic]
        public static void Print()
        {
         }
        [ThreadStatic]
        public static int Value = 0;
    }
    class Program
    {
        static void Main(string[] args)
        {
           
            Console.ReadLine();
        }
    }
}
Hope you liked this article, do write your comments.
Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)