X hits on this document

765 views

0 shares

0 downloads

0 comments

25 / 396

C# LANGUAGE SPECIFICATION

New classes are created using class declarations. A class declaration starts with a header that specifies the attributes and modifiers of the class, the name of the class, the base class (if any), and the interfaces implemented by the class. The header is followed by the class body, which consists of a list of member declarations written between the delimiters { and }.

The following is a declaration of a simple class named Point:

public class Point { public int x, y;

public Point(int x, int y) { this.x = x; this.y = y; } }

Instances of classes are created using the new operator, which allocates memory for a new instance, invokes a constructor to initialize the instance, and returns a reference to the instance. The following statements create two Point objects and store references to those objects in two variables:

Point p1 = new Point(0, 0); Point p2 = new Point(10, 20);

The memory occupied by an object is automatically reclaimed when the object is no longer in use. It is neither necessary nor possible to explicitly deallocate objects in C#.

1.6.1 Members

The members of a class are either static members or instance members. Static members belong to classes, and instance members belong to objects (instances of classes).

The following table provides an overview of the kinds of members a class can contain.

Member

Description

Constants

The constant values associated with the class

Fields

The variables of the class

Methods

The computations and actions that can be performed by the class

Properties

The actions associated with reading and writing named properties of the class

Indexers

The actions associated with indexing instances of the class like an array

Events

The notifications that can be generated by the class

Operators

The conversions and expression operators supported by the class

Constructors

The actions required to initialize instances of the class or the class itself

Destructors

The actions to perform before instances of the class are permanently discarded

Types

The nested types declared by the class

1.6.2 Accessibility

Each member of a class has an associated accessibility, which controls the regions of program text that are able to access the member. There are five possible forms of accessibility. These are summarized in the following table.

12Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views765
Page views765
Page last viewedSat Dec 03 03:08:43 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments