Accessing Attributes and Methods in C#

CsharpServer Side ProgrammingProgramming

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...
raja
Published on 24-Jul-2018 07:52:52
Advertisements