Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Pro .NET 2.0 Code And Design Standards In CSharp (2006) [eng]

.pdf
Скачиваний:
34
Добавлен:
16.08.2013
Размер:
3.43 Mб
Скачать

56 C H A P T E R 3 C O D E D E V E L O P M E N T

Where

The internal modifier is placed in the type declaration.

Why

An internal modifier extends accessibility to assembly or program level.

How

The internal type modifier may be specified as follows:

public class Car

{

//internal modifier internal DoSomething() {;}

}

The Standard: internal Modifier

The standard acknowledges the use of the internal modifier where there is a requirement to limit accessibility to other classes in the same program or assembly.

protected internal

The keyword protected internal is an accessibility declaration used to modify visibility of a type.

What

A protected internal modifier signifies that accessibility is limited to a member(s) within a given class, a class(es) derived from that class, and a nonderived class(es) in the same assembly. The protected internal modifier yields protected or internal accessibility.

Where

The protected internal modifier is placed in the type declaration.

Why

A protected internal modifier offers a little more flexibility than protected by enabling objects that are not part of the class hierarchy but are resident in the same assembly or program to access functionality. It is commonly used to enable access within the given assembly yet allow functionality to appear abstract to another assembly or program.

C H A P T E R 3 C O D E D E V E L O P M E N T

57

How

The protected internal modifier may be specified as follows:

public class Car

{

//protected internal modifier protected internal DoSomething() {;}

}

The Standard: protected internal Modifier

The standard acknowledges the use of the protected internal modifier where there is a requirement to limit accessibility to a class, a class(es) derived from that class, or other classes in the same assembly.

public

The keyword public is an accessibility declaration used to declare that there is no limit to accessibility or visibility of a type.

What

A public modifier signifies that there is no limit to accessibility.

Where

The public modifier is placed in the type declaration.

Why

A public modifier is used when there is no need to limit access to a class. It is commonly used to define and publish the interface of the concrete class (object), which signifies how client code can collaborate with it.

How

The public type modifier may be specified as follows:

public class Car

{

//public modifier public DoSomething() {;}

}

58 C H A P T E R 3 C O D E D E V E L O P M E N T

The Standard: public Modifier

The standard acknowledges the use of the public modifier where there is a requirement not to limit accessibility to functionality by defining and publishing an interface against which client code can collaborate.

static

The keyword static is a type modifier that is used to associate a type with a class.

What

A static type modifier signifies that a given type is a member of the class and not an instance member.

Where

The static type modifier is placed in the type declaration.

Why

A static type modifier is used when it is necessary that a class has a member that may be called independently of instantiation. A static member may be called from a class, without the class having been instantiated. Although the most obvious example is the Main() method, utility classes commonly are used to gain access to static functionality, saving the overhead of instantiating classes (refer to the technique as used in the .NET Framework, for example:

System.Math).

How

The static type modifier may be specified as follows:

public class Client

{

//static type modifier static void Main() {;}

}

The Standard: static Modifier

The standard acknowledges the use of static to differentiate a class member from an object member and also to leverage the ability to access class functionality without the overhead of instantiation.

C H A P T E R 3 C O D E D E V E L O P M E N T

59

Accessibility Summary

Table 3-1 summarizes .NET accessibility.

Table 3-1. .NET Accessibility

Item

Modifier

Class

abstract, internal, sealed, protected, private, and public.

Class members

new, private, protected, internal, protected internal, and public.

Enumeration members

Implicitly public (no access modifiers allowed on member

 

declaration).

Interface members

Implicitly public (no access modifiers allowed on member

 

declaration).

Namespace

Implicitly public (no access modifiers allowed on namespace

 

declaration).

Structure members

Default is private, internal, and public (implicitly sealed).

Types

Default is internal, public (applies to types declared in namespaces or

 

compilation units).

 

 

Tip A class may contain an inner class, which may be modified as private or protected.

Class Fundamentals

We now consider the following class fundamentals: sealed and static modifiers, attribute class, class header, and class members (field, constant, delegate, enumeration, event, constructor, property, and method).

Attribute

The Attribute holds metadata that may be accessed programmatically, at runtime, to interrogate entities within an assembly.

What

An Attribute is a class that derives from class System.Attribute. It holds metadata or declarative information that is accessible, through reflection. An Attribute may be inherited; if, however, that is not desirable, then it may be modified as sealed. There are two types of attributes: intrinsic and custom. Intrinsic attributes are part of the CLR (Common Language Runtime)—for example, [serializable] or [assembly: AssemblyTitle("")]. Custom attributes are roll-your-own attributes.

60 C H A P T E R 3 C O D E D E V E L O P M E N T

Where

An Attribute may be applied to assembly, class, delegate, enum, event, field/member variable, interface, method, module, parameter, property, return value, or struct.

Why

An Attribute is an adornment that is used to add information about an element which may be accessed programmatically, at runtime.

How

Intrinsic attributes are the most commonly used attribute type, and they are specified as follows:

[serializable] class Car {;}

However, where there may be ambiguity, such as in the case of a method (method— default value) or return value (return—default value), a default value is used with the attribute to indicate which element the attribute references, as in the following example:

//declare a method with an attribute using the 'method' default attribute. [method: ThatAttribute] int MethodTwo(int i)

//declare a return value with an attribute using the 'return' default attribute. [return: OtherAttribute] int MethodThree(int i)

The Standard: Attribute

The standard acknowledges the use of Attribute, which may be used to enrich an entity with metadata that may be accessed programmatically, at runtime.

Class Header

The class header is used to define the high-level domain features of the entity, for which it is an abstraction.

What

A class header identifies the class: it contains the class modifier (e.g., public); the keyword class; and the name of the class.

Where

The class header is placed at the top of the class block.

C H A P T E R 3 C O D E D E V E L O P M E N T

61

Why

A class header is used to specify the visibility of the class through its modifier (public); that it is a class type; and that an object instance may be referenced explicitly by its custom type name (e.g., Car).

How

The class header may be specified as follows:

public class Car {;}

The Standard: Class Header

The standard acknowledges the use of class header, which comprises the keyword class, modifier, and class name. It is noted that consideration may be given to using a short and generic class name that is appropriate to the domain.

const

The constant (const) is a class variable whose value is constant, and it is commonly used in simple situations.

What

A const is a static modifier, which is a value type and used on local variables or member fields; once its value is assigned, it can’t be changed at runtime. A const can be used with bool, byte, char, decimal, double, enum, float, int, long, short, string, or a reference type.

Where

The const keyword is placed as the modifier in the type declaration.

Why

A const is a quick solution compared with an enum. It is commonly used in simple situations where there are one or two constants. It may also be used in situations where the underlying value of the constant needs to be a string type—an enum does not offer that functionality. In more complex situations, where there are many constants that are related (e.g., colors), an enum is commonly used.

How

A constant may be specified as follows:

const int wheels = 4;

62 C H A P T E R 3 C O D E D E V E L O P M E N T

The Standard: Constant

The standard acknowledges the use of const in simple situations or where the underlying value has to be a string type.

delegate

The delegate is a convenient way to avoid explicitly committing code to call functionality from a named object. It adds the flexibly to call methods with the same signature in different objects, by delegating the collaboration to a delegate object.

What

A delegate is a class that is a reference to a method that has a given signature (parameter list and return type) and wraps a method.

Where

The delegate keyword is placed after the modifier in the type declaration.

Why

A delegate offers an efficient way to access the functionality of another object. A given delegate may be used, by a containing object to reference different methods from different class types, as long as the signature of the methods is identical to the signature of the delegate. It is commonly used to support events, which are based on a publisher-subscribe model.

How

The delegate may be specified as follows:

public delegate void Change (object sender, EventArgs e);

The Standard: Delegate

The standard acknowledges the use of delegate for programmatic efficiency and flexibility.

enum

The enum (enumeration) is a convenient way to store, extend, and use related constant values.

What

An enum is a distinct value type that contains an enumerator list (a set of named numeric constants). It supports the following underlying types: byte, sbyte, short, ushort, int, uint, long, and ulong. By default its underlying type is int (Int32).

C H A P T E R 3 C O D E D E V E L O P M E N T

63

Where

The enum is commonly used in complex situations where there are many constants that are related (e.g., colors), or where there isn’t a requirement for the underlying value to be a string type (in which case, use a const). It may be placed in the class file or in a separate code file, within the same namespace or referenced.

Why

An enum is an intuitive and convenient way to manage and give context to constant values. (It may also be used to harness the benefit of IntelliSense, in the Visual Studio IDE).

How

The enum may be specified as follows:

enum Radiator

{

Briscoe,

Detroit, McCord

}

Note Each constant value is ended with a comma, except for the last constant value.

The Standard: Enumeration

The standard acknowledges the use of enum in situations that are complex or where a string type is not required as the underlying type of each constant value (otherwise, a const may be used).

event

.NET adheres to an event model based on a publisher-subscriber architecture.

What

The event is a methodology by which a class may raise a notification. It is declared as a delegate class and published by an object against which other objects subscribe by attaching or registering an event handler of the same signature (parameter list and return type) as the delegate type of the event.

Where

An event keyword is placed after the modifier in the type declaration.

64 C H A P T E R 3 C O D E D E V E L O P M E N T

Why

The event is a way by which objects may collaborate. Note: subscribers to an event may register or deregister, at runtime.

How

An event may be specified as follows:

//declare a delegate

public delegate void Alarm (string location);

//declare event of type delegate

public event Alarm OnOverHeating (string location);

The Standard: Event

The standard acknowledges the use of event as a way for objects to collaborate.

Field

The field is a variable that has a type or class level association.

What

A field is associated with a class type or with an instance of a class (i.e., object). A field modified as static is a class field; otherwise, it is an instance field.

Where

The field is commonly placed immediately below the class header, in the body of the class. It is used to hold the state of a property.

Why

Generally, a member field serves three purposes: it stores the underlying value of an object’s property, in which case it is declared private; it is declared as static to hold a value for the class; or it is used as a utility variable to service requirements of the class.

How

A member field may be specified as follows:

public class

{

int count;

}

C H A P T E R 3 C O D E D E V E L O P M E N T

65

The Standard: Field

The standard acknowledges that a field is associated with a class and is commonly used to store the underlying value of a property of an object, or if modified as static to store a value for the class.

Indexer

In the C# language, an indexer is analogous to a default property.

What

The indexer is a special kind of property that enables an object to be indexed. This enables a collection contained within the object to be accessed on the name of the object, using the this keyword.

Where

An indexer is placed in the body of a class, as a special property.

Why

The indexer offers the efficiency of a default property.

How

An indexer may be specified as follows:

public class Car

{

private int[] myArray;

//specify indexer - note the use of this operator. public int this[int index]

{

get{return myArray[index];} set{myArray[index] = value;}

}

}

The Standard: Indexer

The standard acknowledges the use of indexer as a default property.

Соседние файлы в предмете Программирование на C++