Skip to Content
C# 9.0 in a Nutshell
book

C# 9.0 in a Nutshell

by Joseph Albahari
February 2021
Intermediate to advanced
1060 pages
26h 28m
English
O'Reilly Media, Inc.
Content preview from C# 9.0 in a Nutshell

Chapter 3. Creating Types in C#

In this chapter, we delve into types and type members.

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 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.

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;
}

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

There are two popular naming conventions for private fields: camel-cased (e.g., firstName) and camel-cased with an underscore (_firstName). The latter convention lets you instantly distinguish private fields from parameters and local ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# 8.0 in a Nutshell

C# 8.0 in a Nutshell

Joseph Albahari, Eric Johannsen
C# 10 in a Nutshell

C# 10 in a Nutshell

Joseph Albahari
Async in C# 5.0

Async in C# 5.0

Alex Davies

Publisher Resources

ISBN: 9781098100957Errata PageSupplemental Content