Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   The Different Kinds of Type Members

A type can define zero or more of the following kinds of members:

 

ConstantsA constant is a symbol that identifies a never-changing data value. These symbols are typically used to make code more readable and maintainable. Constants are always as- sociated with a type, not an instance of a type. Logically, constants are always static members. Discussed in Chapter 7, “Constants and Fields.”

FieldsA field represents a read-only or read/write data value. A field can be static, in which case the field is considered part of the type’s state. A field can also be instance (nonstatic), in which case it’s considered part of an object’s state. I strongly encourage you to make fields private so that the state of the type or object can’t be corrupted by code outside of the defin- ing type. Discussed in Chapter 7.

Instance constructorsAn instance constructor is a special method used to initialize a new

object’s instance fields to a good initial state. Discussed in Chapter 8, “Methods.”

 

Type constructorsA type constructor is a special method used to initialize a type’s static

fields to a good initial state. Discussed in Chapter 8.


MethodsA method is a function that performs operations that change or query the state of a type (static method) or an object (instance method). Methods typically read and write to the fields of the type or object. Discussed in Chapter 8.

Operator overloadsAn operator overload is a method that defines how an object should be manipulated when certain operators are applied to the object. Because not all program- ming languages support operator overloading, operator overload methods are not part of the Common Language Specification (CLS). Discussed in Chapter 8.

Conversion operatorsA conversion operator is a method that defines how to implicitly or explicitly cast or convert an object from one type to another type. As with operator overload methods, not all programming languages support conversion operators, so they’re not part of the CLS. Discussed in Chapter 8.

PropertiesA property is a mechanism that allows a simple, field-like syntax for setting or querying part of the logical state of a type (static property) or object (instance property) while ensuring that the state doesn’t become corrupt. Properties can be parameterless (very com- mon) or parameterful (fairly uncommon but used frequently with collection classes). Discussed in Chapter 10, “Properties.”

EventsA static event is a mechanism that allows a type to send a notification to one or more static or instance methods. An instance (nonstatic) event is a mechanism that allows an object to send a notification to one or more static or instance methods. Events are usually raised in response to a state change occurring in the type or object offering the event. An event con- sists of two methods that allow static or instance methods to register and unregister interest in the event. In addition to the two methods, events typically use a delegate field to maintain the set of registered methods. Discussed in Chapter 11, “Events.”



TypesA type can define other types nested within it. This approach is typically used to break

a large, complex type down into smaller building blocks to simplify the implementation.

 

Again, the purpose of this chapter isn’t to describe these various members in detail but to set the stage and explain what these various members all have in common.

Regardless of the programming language you’re using, the corresponding compiler must process your source code and produce metadata and Intermediate Language (IL) code for each kind of mem- ber in the preceding list. The format of the metadata is identical regardless of the source programming language you use, and this feature is what makes the CLR a common language run time. The metadata is the common information that all languages produce and consume, enabling code in one program- ming language to seamlessly access code written in a completely different programming language.

This common metadata format is also used by the CLR, which determines how constants, fields, constructors, methods, properties, and events all behave at run time. Simply stated, metadata is the key to the whole Microsoft .NET Framework development platform; it enables the seamless integra- tion of languages, types, and objects.


The following C# code shows a type definition that contains an example of all the possible mem- bers. The code shown here will compile (with warnings), but it isn’t representative of a type that you’d normally create; most of the methods do nothing of any real value. Right now, I just want to show you how the compiler translates this type and its members into metadata. Once again, I’ll discuss the individual members in the next few chapters.

 

using System;   public sealed class SomeType {   //  
  // Nested class private class SomeNestedType { }     //    
  // Constant, read­only, and static read/write field private const Int32 c_SomeConstant = 1;     //    
private readonly String m_SomeReadOnlyField = "2"; //
private static Int32 s_SomeReadWriteField = 3; //
  // Type constructor static SomeType() { }     //    
  // Instance constructors public SomeType(Int32 x) { }     //    
public SomeType() { } //
  // Instance and static methods private String InstanceMethod() { return null; }     //    
public static void Main() {} //
  // Instance property public Int32 SomeProp {     //    
get { return 0; } //
set { } } //
  // Instance parameterful property (indexer) public Int32 this[String s] {     //    
get { return 0; } //
set { } } //
  // Instance event public event EventHandler SomeEvent;     //    
}    

If you were to compile the type just defined and examine the metadata in ILDasm.exe, you’d see

the output shown in Figure 6-1.

 

Notice that all the members defined in the source code cause the compiler to emit some meta- data. In fact, some of the members cause the compiler to generate additional members as well as additional metadata. For example, the event member (17) causes the compiler to emit a field, two methods, and some additional metadata. I don’t expect you to fully understand what you’re seeing here now. But as you read the next few chapters, I encourage you to look back to this example to see how the member is defined and what effect it has on the metadata produced by the compiler.


FIGURE 6-1ILDasm.exe output showing metadata from preceding code.

 


Date: 2016-03-03; view: 606


<== previous page | next page ==>
Nbsp;   The dynamic Primitive Type | Nbsp;   Type Visibility
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)