Jon Rumsey

An online markdown blog and knowledge repository.


Project maintained by nojronatron Hosted on GitHub Pages — Theme by mattgraham

DotNET CSharp Stuff

Notes on various topics while developing software with, or learning more about, C# and .NET.

Table of Contents

Custom Extensions aka Extension Methods

Custom extension methods can be created for any class.

Using extension methods via:

Extension Method Binding:

The Compiler Ingestion and Processing Order:

  1. Extended Type's method signatures.
  2. Extended Interface method abstractions.
  3. Extension Method Type method signatures.

Once the Compiler finds a signature match, it stops working its way down this list.

What Are Extension Methods

Extension Methods Are:

Examples of Existing Extension Methods:

Note: Extension Methods can help create cleaner code.

How To Build An Extension Method

To build an extension method:

How To Use An Extension Method

  1. Call the Extension Class into scope with a using directive.
  2. Call the Extension Method as if it were the target Type's instance method.

When to Use Extension Methods

Risks Using Extension Methods

Code you don't control might change unexpectedly, causing functionality our input/output changes your Extension Method cannot support, or method signatures silently override your Extension Method(s).

Why To Use Extension methods Or Not

Collection Pattern:

  1. Define a Collection Class that implements IEnumerable<T>.
  2. Build functionality around this custom class like Add, Remove, Find, etc.

Collection Functionality using Extension Methods:

  1. Build Extension Methods that have the functionality necessary to operate on IEnumerable<T> interfaces.
  2. Bring-in the Extension Namespace to use when a type that IEnumerable<T> is in scope.

Extension Collections Benefit:

Layered Application Design Pattern:

  1. Design Data Transfer Objects with little functionality.
  2. Implement object translation between application boundaries as needed.

Layered Applications Leveraging Extension Methods:

  1. Design Domain Entities (same as above) with little-to-no functionality.
  2. Add Extension methods to add the functionality that is specific to each Application Layer.

Layer Application Extension Methods benefits:

Chain Your Method Calls!

Avoid Nested Method Calls!

Separate Dependencies from Classes that don't need them:

Avoid:

Do use Extensions Methods to:

About Aggregation and Composition

Create larger, more complex Types by piecing together existing, smaller and less complex Types.

Both:

Aggregation

Composition

Enable a Class to utilize another class

With inheritance, a base class is used to store the state data, simplifying definition and management of inheriting class ojects.

Use Composition to help overcome issues with inheritance, especially as inheritor complexity requirement rises.

Favor Composition Over Inheritance

Inheritance is fine, but as complexity increases, inheritance limitations become a barrier to further development.

Composition enables continued added complexity with less repetitive code, guaranteeing valid object generation through factory methods.

For ForEach While DoWhile

For: Use this to iterate over an indexed collection.

ForEach: Syntactic-sugar for GetEnumerator and Next calls on an IEnumerable collection.

While: Execute code within the attached code block so long as the condition returns true.

Do-While: Execute code within the attached code block and then check the conditional, and only execute the codeblock if the condition returns true.

Null Safety in CSharp Overview

Null detection and handling changes in C# 2.0 and greater provide a means to better avoid null reference exceptions in an app.

C# 2.0 introduced Nullable<T> where a generic reference type could be initialized as null. Use T? to implicitly or explicitly set a null:

C# 8.0 allows setting intent as in the reference type might be null or is always non-null (default value). The compiler tries to enforce this setting.

When a null is 'dereferenced', it means the variable is evaluated at runtime but refers to an initially null value.

Null safety reduces the possibility of NullReferenceException occurences, so the compiler provides warnings when possibly derefencing null.

Basically, that last point is the goal: Help to avoid throwing NullReferenceException whenever possible.

Setting Nullability In Your Code

To infer intent of code and enforce desired behavior, set "Nullable Context", using these contexts:

Settings are available:

Null Operators

There are several operators dedicated to working with nullable references:

Conditional operator (ternary) ?:

null forgiving operator !:

null coalescing operator ??:

null conditional operators .? and ?[]:

Best Practices Handling Nullability

References

Return to ContEd Index

Return to Root README