Classes
A class is the most common kind of reference type. The simplest possible class declaration is as follows:
class YourClassName { }
A more complex class optionally has:
preceding the keyword |
attributes and class modifiers. The non-nested class modifiers are |
following |
generic type parameters, a base class, and interfaces |
within the braces |
class members (these are methods, properties, indexers, events, fields, constructors, operator functions, nested types, and a finalizer) |
Fields
A field is a variable that is a member of a class or struct. For example:
class Octopus {string name;
public int Age = 10;
}
A field may have the readonly
modifier to prevent
it from being modified after construction. A read-only field can be assigned only in its
declaration or within the enclosing type’s constructor.
Field initialization
Field initialization is optional. An uninitialized field has a default value
(0, \0, null, false
). Field initializers run before
constructors:
string name = "anonymous";
Declaring multiple fields together
For convenience, you may declare multiple fields of the same type in a comma-separated list. This is a convenient way for all the fields to share the same attributes and field modifiers. For example:
static readonly int legs = 8, eyes = 1;
Methods
A method performs an action in a series of statements. A method can receive input data from the caller by specifying parameters, and output ...
Get C# 3.0 Pocket Reference, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.