C# Class

Use class instances. A class is a reference type that is instantiated with a constructor.
Class. Far in the distance a castle stands. Fog settles in the area. This castle guards the nearby town—its deep moat protects from invaders.
Class keyword. A class in C# guards its information. Like the castle it keeps its treasure away from outsiders. Classes are everywhere in this language.Keywords
A start. This program defines a class named Box, with one public method Open(). This class is a type, a template, that does nothing.

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

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.Generics
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.Public, privateProtected, internal
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.Readonly
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
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.

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

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

A summary. With classes, we create instances of custom types. This gives a great power to model data. With models, we tame the dragon of complexity (a formidable beast).
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls