Thursday, December 15, 2011

A Programmer’s Introduction to C#, Second Edition

By Eric Gunnerson
apress.com
IN MOST PROGRAMMING LANGUAGES, some information is expressed through declaration, and other information is expressed through code. For example, in the following class member declaration
public int Test;
the compiler and runtime will reserve space for an integer variable and set its protection so that it is visible everywhere. This is an example of declarative information; it’s nice because of the economy of expression and because the compiler handles the details for us.
Typically, the types of declarative information are predefined by the language designer and can’t be extended by users of the language. A user who wants to associate a specific database field with a field of a class, for example, must invent a way of expressing that relationship in the language, a way of storing the relationship, and a way of accessing the information at runtime. In a language like C++, a macro might be defined that stores the information in a field that is part of the object. Such schemes work, but they’re error-prone and not generalized. They’re also ugly.
The .NET Runtime supports attributes, which are merely annotations that are placed on elements of source code, such as classes, members, parameters, etc. Attributes can be used to change the behavior of the runtime, provide transaction information about an object, or convey organizational information to a designer. The attribute information is stored with the metadata of the element and can be easily retrieved at runtime through a process known as reflection.
C# uses a conditional attribute to control when member functions are called. A use for the conditional attribute would look like this:
using System.Diagnostics;
class Test
{
[Conditional("DEBUG")]
public void Validate()
{
}
}
Most programmers will use predefined attributes much more often than writing an attribute class.........


This file has been Downloaded 4855 times

No comments:

Post a Comment

Check Page Rank of your Web site pages instantly:

This page rank checking tool is powered by Page Rank Checker service