Info Sobre .Net

Páginas: 28 (6966 palabras) Publicado: 27 de julio de 2012
The C# Type System
Don Box Cofounder, DevelopMentor http://www.develop.com/dbox

1

Outline • Part 1: Type construction • Part 2: Types at runtime • Part 3: Delegates and events

2

The role of type • Types are the fundamental unit of design, implementation, and execution in .NET – The CLR manages type definitions at runtime – Types package related code into reusable abstractions – Allobjects and values are instances of types – CLR-based compilers map language abstractions into CLR type definitions – Several well-known base types and attributes distinguish structs, enums, typees, interfaces

3

Figure 2.1: Distinguishing types via flags and base types

4

Figure 2.2: Type definitions in C#

public public public public public

class interface struct enum delegatevoid

TheClass {} TheInterface {} TheStruct {} TheEnum { } TheDelegate();

5

Anatomy of a type • Type definitions are composed of zero or more member declarations that make the type useful – Fields have storage and hold values of a given type – Methods have signatures and code and represent operations – Constructors are special methods invoked at init-time – Properties and events are hintsabout method semantics – Nested types provide implementation support for their surrounding type – Access to members can be restricted to intra-type or intraassembly for encapsulation

6

Figure 2.3: Type members in ILDASM

Field Constructor Method Event Property Nested Type

7

Figure 2.4: Type member definitions in C#

public class TheType { public int TheField; public TheType() {}public void TheMethod(int myparam) {} public event System.EventHandler TheEvent; public int TheProperty { get { return 0; } set { }} public class TheNestedType{} }

8

Figure 2.5: Type members in ILDASM revisited

Field Constructor Method

Event Property Nested Type

Property methods

Event methods

Compiler-synthesized field for event

9

Figure 2.6: Access modifiers

C#Type public internal public internal Member protected protected internal private

VB.NET Public Private Public* Friend Protected Protected Friend Private*

Meaning Type is visible everywhere Type is only visible inside of declaring assembly Member is visible everywhere Member is only visible inside of declaring assembly Member is only visible inside of declaring type and its subtypes Member isonly visible inside of declaring type and its subtypes or other types inside of assembly Member is only visible inside of declaring type

* VB.NET defaults to Public for methods and Private for fields declared using the Dim keyword

10

Fields • Fields are named units of typed storage that are members of a type – Fields must have a locally-unique name – Fields must be affiliated with someaccessible type – The CLR allocates/manages memory for fields – Fields with no explicit initializer are set to zero by default (unlike locals) – Fields marked readonly or const cannot be modified once initialized – Fields marked const must be initialized using compile-time values

11

Figure 2.9: Const and readonly fields

public type Patient { // legal, initial value known at compile-timepublic const int MAX_PATIENT_AGE = 32 * 4; // legal, value not needed at compile-time public readonly int SOCIAL_SEC_NUMBER = GetNewSSNo(); // legal, value not needed at compile-time public double age = GetAgeAtBirth(); // illegal, initial value not known at compile-time public const int MAX_ID = GetMaxID(); } void Main() { Patient pt = new Patient(); // illegal, field is readonlypt.SOCIAL_SECURITY_NUMBER = -1 ; // illegal, field is const pt.MAX_PATIENT_AGE = -1; // legal, age is neither const or readonly pt.age = 32.0; }

12

Methods • Operations on a type are exposed using method declarations – Methods have a name and an optional return type – Methods have a (potentially empty) list of typed, named, parameters – Methods typically have executable code associated with them – Methods...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Todo Sobre Ado .Net
  • IMPUESTO SOBRE ACTIVO NETO
  • Cuestionario sobre .net
  • Info Sobre Violeta Se Fue a Los cieLos
  • Info Sobre Virus
  • Info Sobre La SEMIOLOGIA
  • Info sobre nebamon
  • Info Sobre Tempietto Bramante

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS