C# Class

Array Class Collections File Keyword String .NET ASP.NET Cast Compression Data Delegate Directive Enum Exception If Interface LINQ Loop Method Number Process Property Regex Sort StringBuilder Struct Switch Time Windows WPF


Classes. Imagine this. A program is an abstract machine.
As it executes,
its parts are in movement,
in constant motion,
moving towards a result. Its parts are called classes.


Models. These building blocks stand alone,
but together,
they form complex models. In this conceptual mirror our creation moves. With classes our abstract machine stores data and thinks.

Constructor: create new

Constructor. We often need to have different ways of instantiating a class. We might use the class in many places. With overloading, we can add many entry points for creating the class.

This keyword

This, base. In class bodies, we use the "this" keyword before a field, method or property identifier. It is an instance expression. We use "base" to indicate a parent instance.

ThisBase vs. This
Object keyword

Object. Every class is derived from the ultimate base class—object. Because of this, every variable can be cast to an object reference. We treat any variable as one type.


Namespaces are an organizational feature. Often, programs will have namespaces containing their classes. This changes the syntax. It alters how we must identify a target class.


Static. The word "static" refers to a position that is unchanging, fixed. A static class cannot be instantiated. Its position in memory is therefore fixed in one place.

Static ClassStatic
About part

Static constructors are not exclusive to static classes. But they are often used there. They execute code when a static class is first used. Static constructors mask some complexity.

Static Constructor

Inheritance is a key feature of the class model. We use inheritance to simplify a program—to make it easier to add new features.
We explore inheritance,
type derivation
and polymorphism.

About part

Polymorphism. This is a good word to make yourself sound smart. And that has certain benefits. But it also refers to a way we use many "forms" of a single object.


Private, public. Classes are by default private. Programmers are less likely to misuse private classes. When we make a class public, we can instantiate it in external locations.

Class shapes

Nested. We show how to instantiate a nested class. The nested class must be public. A nested class is determined by the lexical position of type declarations in a source file.

Nested Class

Properties are an important feature. They give us a way to add executable code in a syntax form that resembles a simple memory access. They have other uses, such as in data-binding.


Indexer. An indexer is a type of property. It is used with simple syntax—the same syntax used for array accesses. This is a form of syntactic sugar.

Copy: new object copied

Fields. Classes contain fields. In fields, we access memory that stores data for each class instance. Fields have modifiers. They can be initialized in a variable initializer.

ReadonlyPublic Static ReadonlyVariable Initializer

Ordering. Does the ordering of fields affect memory usage? I investigate further. It does not matter how we order our fields. We discover a virtualized type system.

Field Ordering
Operator keyword

Operators. For some types, it make sense to overload operators. An overloaded operator can make syntax simpler. We can add classes together. We can use unary or binary operators.

Generic type

Generics. If we see the < and > tokens in a class declaration, it is a generic class. Those tokens contain the type parameters to the class. Generic (like Dictionary) are powerful.


Modifiers. These influence how a class may be used. A public class can be anywhere in a project. A private one is more restricted. An abstract class is used as a template for other classes.

Partial keyword

Partial. A partial class is contained in more than one file. This is a feature used often by Visual Studio in Windows Forms. It is not that important.


Sealed. The sealed keyword is used to specify that a class cannot be derived from. It has performance benefits, but it is not critical to understanding the language.


Abstract. An abstract class cannot be directly instantiated.
It is used,
in derivation,
to create other classes. Unlike abstract art, we may eventually understand this.

Attribute Flags syntax

Attribute. This is attached to a type such as a class. It becomes part of the metadata. It is used to influence the compiler's behavior on the type. Types are changed with no runtime cost.


OOP, patterns. What is the point of object-orientation? I describe why we would want to use this style of programming in projects. I also explore some concepts and design patterns of OOP.

C# programming language

With classes, we create instances of custom types. This gives an incredible power to model data.
With models,
we tame the dragon of complexity,
a formidable beast.