C#:.NET

.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda Parse Path Process Property Regex Replace Row Sort Split Static StringBuilder Substring Switch Tuple Window

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

First: This program defines a class named Box, with one public method Open(). This class is a type, a template, that does nothing.

Methods

Main: The program begins in the Main method. Here the Box is created with "new" and Open() is called, printing a message.

C# program that uses class

class Box
{
    public void Open()
    {
	System.Console.WriteLine("Box opened");
    }
}

class Program
{
    static void Main()
    {
	// The program begins here.
	// ... Create a new Box and call Open on it.
	Box box = new Box();
	box.Open();
    }
}

Output

Box opened
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.Constructor
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
Generics. A generic class is compiled for a certain parameter type, one specified in angle brackets. Generics (like Dictionary) are powerful. They are often faster than alternatives.GenericsGenerics: Collections
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.Namespace
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.Object
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
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.Inheritance
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.PrivatePublicProtectedInternal
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. These 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 uses in data-binding.Property
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.Indexer
Readonly. This modifier is used to denote a field that can be initialized, but not changed later in program execution. A readonly field is initialized at runtime.ReadonlyReadonly: public static
Variable initializer. We can assign fields directly in their class declarations. The C# compiler automatically moves the initialization to the constructor.Variable Initializer
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.Operator
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.Partial
Sealed. This 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.Sealed
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.Abstract
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.Attribute
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.OOPFactoryProxy
Performance. Classes are fast. They are passed to methods as small references (4 or 8 bytes). Few bytes need to be copied each time. With class pointers, we efficiently store many objects.

Constructors: It tends to be fastest to use overloaded constructors to assign values in a newly-created class.

Constructor, Benchmark

Ordering: Does the ordering of fields affect memory usage? I investigate further. It does not matter how we order our fields.

Field Ordering

Sealed: This keyword can enable some optimizations in the .NET Framework. But this is a small improvement (at best).

Sealed

Structs: These are not faster than classes. Often, structs are slower because they incur more copying with method calls.

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