May 2020
Intermediate to advanced
1099 pages
27h 13m
English
In this chapter, we delve into types and type members.
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 the following:
Preceding the keyword class |
Attributes and class modifiers. The non-nested class modifiers are public, internal, abstract, sealed, static, unsafe, and partial |
Following YourClassName |
Generic type parameters and constraints, a base class, and interfaces |
| Within the braces | Class members (these are methods, properties, indexers, events, fields, constructors, overloaded operators, nested types, and a finalizer) |
This chapter covers all of these constructs except attributes, operator functions, and the unsafe keyword, which are covered in Chapter 4. The following sections enumerate each of the class members.
A field is a variable that is a member of a class or struct; for example:
class Octopus
{
string name;
public int Age = 10;
}
Fields allow the following modifiers:
| Static modifier | static |
| Access modifiers | public internal private protected |
| Inheritance modifier | new |
| Unsafe code modifier | unsafe |
| Read-only modifier | readonly |
| Threading modifier | volatile |
The readonly modifier prevents a field from being modified after construction. A read-only field can be assigned only in its declaration or within the enclosing type’s constructor.
Field ...
Read now
Unlock full access