From the course: Advanced C#: Functional Programming Patterns

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Replace VOID with the UNIT class

Replace VOID with the UNIT class - C# Tutorial

From the course: Advanced C#: Functional Programming Patterns

Replace VOID with the UNIT class

In this chapter, we'll explore additional types that promote a more functional coding style. Here are the three key types we'll explore. Option represents an optional value, providing a safer alternative to using null. Either encapsulates a value that can be either one of two possible types. Commonly used for error handling. Unit represents a type that signifies the absence of a meaningful value. Similar to using void, but as a type. We'll look at Unit in this video. Let's start by understanding the challenges with using void methods. Void methods do not return any value which is at odds with functional programming principle of functions always returning values. This limitation hampers the ability to compose functions and chain operations, thereby reducing code reusability and expressiveness. Void methods do not provide any type information about their result, which can lead to less predictable and less type-safe code. Since void is a keyword in C#, not a type, it cannot be used in…

Contents