Udostępnij za pośrednictwem


Type Members

The runtime allows you to define members of your type: events, fields, nested types, methods, and properties. Each member has a signature. The following table describes the type members used in the .NET Framework.

Member

Description

Event

Defines an incident that can be responded to, and defines methods for subscribing to, unsubscribing from, and raising the event. Events are often used to inform other types of state changes.

Field

Describes and contains part of the type's state. Fields can be of any type supported by the runtime.

Nested type

Defines a type within the scope of the enclosing type.

Method

Describes operations available on the type. A method's signature specifies the allowable types of all its arguments and of its return value.

A constructor is a special kind of method that creates new instances of a type.

Property

Names a value or state of the type and defines methods for getting or setting the property's value. Properties can be primitive types, collections of primitive types, user-defined types, or collections of user-defined types. Properties are often used to keep the public interface of a type independent from the type's actual representation.

Member Characteristics

The common type system allows type members to have a variety of characteristics, but languages are not required to support all these characteristics. The following table describes these member characteristics.

Characteristic

Can apply to

Description

abstract

Methods, properties, and events

The type does not supply the method's implementation. Types that inherit abstract methods and types that implement interfaces with abstract methods must supply an implementation for the method. The only exception is when the derived type is itself an abstract type. All abstract methods are virtual.

private, family, assembly, family and assembly, family or assembly, or public

All

Defines the accessibility of the member:

private

Accessible only from within the same type as the member or within a nested type.

family

Accessible from within the same type as the member and from derived types that inherit from it.

assembly

Accessible only in the assembly in which the type is defined.

family and assembly

Accessible only from types that qualify for both family and assembly access.

family or assembly

Accessible only from types that qualify for either family or assembly access.

public

Accessible from any type.

final

Methods, properties, and events

The virtual method cannot be overridden in a derived type.

initialize-only

Fields

The value can only be initialized, and cannot be written after initialization.

instance

Fields, methods, properties, and events

If a member is not marked as static (C# and C+), Shared (Visual Basic), virtual (C# and C+), or Overridable (Visual Basic), it is an instance member (there is no instance keyword). There will be as many copies of such members in memory as there are objects that use it.

literal

Fields

The value assigned to the field is a fixed value, known at compile time, of a built-in value type. Literal fields are sometimes referred to as constants.

newslot or override

All

Defines how the member interacts with inherited members with the same signature:

newslot

Hides inherited members with the same signature.

override

Replaces the definition of an inherited virtual method.

The default is newslot.

static

Fields, methods, properties, and events

The member belongs to the type it is defined on, not to a particular instance of the type; the member exists even if an instance of the type is not created, and it is shared among all instances of the type.

virtual

Methods, properties, and events

The method can be implemented by a derived type and can be invoked either statically or dynamically. If dynamic invocation is used, the type of the instance that makes the call at run time determines which implementation of the method is called, rather than the type known at compile time. To invoke a virtual method statically, the variable might need to be cast to a type that uses the desired version of the method.

Overloading

Each type member has a unique signature. Method signatures consist of the method name and a parameter list (the order and types of the method's arguments). More than one method with the same name can be defined within a type as long as the signatures differ. When two or more methods with the same name are defined, the method is said to be overloaded. For example, in System.Char, IsDigit is overloaded. One method takes a Char and returns a Boolean. The other method takes a String and an Int32, and returns a Boolean. Parameter lists can also be qualified by the varargs constraint, which indicates that the method supports a variable argument list.

Inheriting, Overriding, and Hiding Members

A derived type inherits all members of its base type; that is, these members are defined on and available to the derived type. The behavior or qualities of inherited members can be modified in two ways:

  • A derived type can hide an inherited member by defining a new member with the same signature. This might be done to make a previously public member private or to define new behavior for an inherited method that is marked as final.

  • A derived type can override an inherited virtual method. The overriding method provides a new definition of the method that will be invoked based on the type of the value at run time rather than the type of the variable known at compile time. A method can override a virtual method only if the virtual method is not marked as final and the new method is at least as accessible as the virtual method.

See Also

Concepts

Type Definitions

Other Resources

Common Type System

Member Design Guidelines

Type Design Guidelines