Understanding Class Performance in C#

Admir Mujkic
5 min readMay 20, 2024

Performance cost of class structures is important when it comes to software development. This article presents the outcomes of benchmark tests conducted to assess different types of class structures.

Theory against Benchmark results. A Benchmark Analysis.

Specifically, standard class, sealed class, derived class, and extended class concepts are tested, and from an analytical perspective, performance implications of each category int he C# language and .NET.

Hence, this article offers a summary of the benchmark results and substantiation on how the mentioned class structures can be used for implementation.

Understanding Class Performance in C#

Benchmark Setup

The benchmark test was conducted using the BenchmarkDotNet library in C#. The classes tested include:

  1. Standard Class: A basic class with a property and a method to get that property.
  2. Sealed Class: A class that cannot be inherited, with similar structure to Standard Class.
  3. Abstract Base Class and Derived Class: An abstract base class with a virtual method and a derived class that overrides this method.
  4. Extended Standard Class: A class that inherits from Standard Class and adds an additional property.

Benchmark Code

The benchmark code sets up several classes and measures the time it takes to access their properties. Below is a breakdown of each part of the code.

Standard Class

A simple class with a single property and a method to return that property.

Sealed Class

A class that cannot be inherited, with the same structure as Standard Class.

Abstract Base Class and Derived Class

An abstract base class with a virtual method and a derived class that overrides this method.

Extended Standard Class

A class that inherits from Standard Class and adds an additional property.

Benchmark Execution

The Class Benchmark class sets up instances of each class and benchmarks the time it takes to access their properties. If you would like to see detailed code and results, please visit: https://dotnetbenchmarks.com/benchmark/1093, a benchmark platform developed by Dave Callan.

Benchmark Results

The results of the benchmark provide insights into the performance of each class type.

The results of the benchmark

Standard Class

The StandardClass represents the most straightforward form and accessible type of class in C#. It only has one property and one way to get the value for that property — the get method. According to the benchmark, the average access time of this class is 0.0035 nanoseconds.

This accessible class has high efficiency due to its simplicity: it does not have any other modifiers or inheritance. Because it does not have any additional complexity or overhead, it works perfectly in performance-critical scenarios, where its simple structure and hierarchy are not needed.

Sealed Class

SealedClass is slightly more complex than StandardClass and has a sealed keyword. It is a class that prevents other classes from inheriting it. However, its benchmark was slightly worse: sealed class property access requires 0.0294 nanoseconds.

There is a slight loss, but this performance factor is essential when considering performance-critical applications. The sealed class source code is more finished and safer, but the compiler’s optimization for sealed classes slightly affects the performance.

Derived Class

DerivedClass is a class with inheritance and polymorphism. It inherits virtual base AbstractBaseClass class and overrides the virtual method. Its benchmark test was 0.2313 nanoseconds, indicating a performance decrease due to complex virtual method call definition and polymorphism overhead.

Virtual methods require additional checks and indirections, slowing the process for deriving classes. Although it is essential for abstraction and polymorphism, developers note that performance is reduced.

Extended Standard Class

ExtendedStandardClass extends StandardClass and has an additional property. This class has a different access time: 0.0048 nanoseconds for the standard property; and 0.0104 nanoseconds, an additional property.

Both added times are results of additional complexity. In performance terms, every new property or method addition can worsen performance, and the developer should consider adding functionality effects.

Usage Guidelines

The StandardClass has simple objects and no inheritance used to remain as fast as possible. It should be used to read the simple data structures, favorite property, and fast information access.

SealedClass should use if the potency does not include inheritance. IT is more favorable for you to use the SealedClass as interpretations in the safety feature.

The performance will not be slow, but seal classes will improve your coding development from misuse.

The usage of the class that includes inheritance, overriding some of the parent classes. Encapsulation will also be highly considered. It is proper to use these classes, but the performance cost will be expensive.

ExtendedStandardClass was derived and similar to other classes. It is used when properties are added to other classes while performing certain operations. Performance costs can be the result of adding new properties. However, the extended class is useful because it can be inserted for example, events.

For the End

Usage of virtual methods introduces additional checks and indirection that can impair the performance of the method. The access to the method table involves performance overhead that degrades the performance of virtual methods.

Usage of inheritance introduces overheads to the class hierarchies. The management and access of properties of the parent class across the hierarchies degrades the performance of the inheritance structure.

When creating C# classes, performance is essential to ensure that all the class structures are associated with optimum class. For use cases, the standard class has the best performance since they lack complexity.

While sealed classes are with additional performance suit five other aspects, the derive classes are with additional performance since they are associated with inheritance and polymorphism concepts. The extended class has some functionality added at the cost of performance.

For a better-oriented application, the developer should ensure that they must have a performance-oriented system or flexible and abstract system for your design.

If you would like to check the full code, please visit the following link.

--

--

Admir Mujkic

Admir combined engineering expertise with business acumen to make a positive impact & share knowledge. Dedicated to educating the next generation of leaders.