Attribute-Based Programming

It is often desirable to expose certain aspects of the code such as architectural constraints, behaviors, features, and so forth, in a nonprocedural way. You are already familiar with C++ and C# keywords such as public and private. These keywords further define the behavior of the class members by describing their accessibility to other classes. .NET lets you define additional aspects of a programming element, such as types and fields, by means of annotating the entity with attributes. The following code excerpt illustrates this idea:

// Project Attributes

[ObsoleteAttribute("Please don't use this method")]
public static int Add(int x, int y) {
  return x + y;
}

public static void Main() {
  int z = Add(10,20);
}

In ...

Get .NET Programming: A Practical Guide Using C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.