Using Attributes in C#

Naimishforu
Posted by in C# category on for Intermediate level | Points: 250 | Views : 6548 red flag
Rating: 5 out of 5  
 1 vote(s)

In this article I shall demonstrate how to use attributes in C#

Introduction


After finishing the Part 2, you decide to create a program that uses the attributes capabilities of C#.

Step 1 of 4

Some of the methods in your program will only run under certain conditions.

Enter the line of code that lets you define a symbol called "DEBUGGING".

MISSING CODE
using System;
Result

The symbol is defined with the line #define DEBUGGING.

Step 2 of 4

You decide to use the Conditional intrinsic attribute to mark some methods that will only be used for debugging.

Type the name of the .NET namespace in which the Conditional attribute is defined.

#define DEBUGGING
using System;
using MISSING CODE;
using System.Runtime.InteropServices;
Result

You type System.Diagnostics. The Conditional attribute is defined in this namespace.

Step 3 of 4

The first method in this assembly will execute only when the program is debugging.

Enter the code that will tell the compiler that this code should only be run during debugging.

#define DEBUGGING
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace CLRAttributes
{
  publich class MainApplication
  {
     MISSING CODE
  }
}
Result

Conditional methods must be marked with the Conditional attribute and the circumstances under which they will execute. So in this case, you enter the code [Conditional("DEBUGGING")].

Step 4 of 4

The Conditional attribute is only one of the intrinsic attributes defined in C#.

The MessageBox method uses a function that is not defined in the .NET library. Which attribute should mark it?

#define DEBUGGING
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace CLRAttributes
{
    public class MainApplication
    {        
       [Conditional("DEBUGGING")]
       public static void DisplayMessage()
       {
           Console.WriteLine("Starting Main method");
       }
	   
       public static extern int MessageBox(int hWnd, string strMessage, string strCaption, int uiType);   

       public static void Main ()
       {
			DisplayMessage();
			MessageBox(0,"Hello from Win API","User32.dll MessageBox",0);
			Console.ReadLine();
       }
   }
}
Result

The DllImport attribute is used when a method must use functions that are not defined in the .NET library. The DllImport attribute provides the information needed to call a function exported from an unmanaged DLL. As a minimum requirement, you must supply the name of the DLL containing the entry point.

Thanks for reading.  Here I have finished the total three parts of Writing an advance C# Programs.


Page copy protected against web site content infringement by Copyscape

About the Author

Naimishforu
Full Name: Naimish Pandya
Member Level: Bronze
Member Status: Member,MVP
Member Since: 1/22/2011 7:38:35 AM
Country: India
Thanks, Naimish http://dotnetinterview.zxq.net/ http://naimishpandya.blogspot.in/
http://naimishpandya.blogspot.in/

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)