Accessing Attributes and Methods in C#


An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc. in your program.

 To set an attribute −

[attribute(positional_parameters, name_parameter = value, ...)]
Element

Here, the name of the attribute and values come inside [ ] positional parameters allow you to specify information.

Example

The following is an example to access attribute and methods in C# −

Live Demo

#define DEBUG
using System;
using System.Diagnostics;
public class Demo {
   [Conditional("DEBUG")]
   public static void Message(string str) {
      Console.WriteLine(str);
   }
}
class Test {
   static void functionDisplay() {
      Demo.Message("Our function...");
   }
   public static void Main() {
      Demo.Message("This is Main function!");
      functionDisplay();
      Console.ReadKey();
   }
}

Output

This is Main function!
Our function...

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 19-Jun-2020

215 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements