Does or will C include features for side-effects verification?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
C# has long been a language appreciated by developers for its robust features and versatile use cases. However, the evolving needs of software engineering, especially in areas like functional programming, have put the spotlight on features like side-effects verification. This article discusses whether or how C# includes features for verifying side effects, along with technical explanations and examples.
Understanding Side Effects
Before delving into side-effects verification, it's important to first comprehend what side effects are in the realm of programming. In simple terms, a side effect occurs when a function or expression modifies some state outside its local environment, such as:
- Modifying a global variable.
- Mutating an input argument.
- Performing I/O operations like printing or reading from a file.
Though sometimes unavoidable, side effects can lead to unpredictable outcomes and bugs if not managed correctly.
Side-Effects Verification in C#
As of the latest releases up to October 2023, C# does not inherently support side-effects verification in a manner akin to some functional languages like Haskell, which have purity as a core component, or Rust, which emphasizes ownership and borrowing.
Existing Tools & Patterns
However, there are patterns and third-party tools that C# developers can use to limit or track side effects:
- Immutable Types: C# supports immutability via `readonly` fields and properties, and the use of immutable collections like `ImmutableList`````<T>``````. This reduces the likelihood of unintended side effects by preventing states from being modified once set.
- Effect Systems: Though not currently present, a framework to annotate and verify effects in a manner somewhat akin to Rust's borrowing might be conceivable.
- Pattern Annotations: Enhanced metadata or attributes that declare and verify function purity could be added. This could warn developers if a "pure" function (one without side effects) accidentally performs actions causing a side effect.
Related reading
- Does Parallel.ForEach limit the number of active threads?
- Does Parallel.ForEach limit the number of active threads?
- Does String.GetHashCode consider the full string or only part of it?
- Does Task.ContinueWith capture the calling thread context for continuation?
- Does the use of the Async suffix in a method name depend on whether the 'async' modifier is used?
- Does WebClient.DownloadFileAsync overwrite the file if it already exists on disk?
- dotnet core System.Text.Json unescape unicode string
- ''dotnet restore'' vs. ''nuget restore'' with TeamCity

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.