Archives
-
C# Functional Programming In-Depth (15) Pattern matching
Pattern matching is a common feature in functional languages. C# 7.0 introduces basic pattern matching, including constant value as pattern and type as pattern, and C# 7.1 supports generics in pattern matching.
-
C# Functional Programming In-Depth (14) Asynchronous Function
Asynchronous function can improve the responsiveness and scalability of the application and service. C# 5.0 introduces async and await keywords to greatly simplify the async programming model.
-
C# Functional Programming In-Depth (13) Pure Function
Functional programming encourages modeling operations with pure functions.
-
C# Functional Programming In-Depth (12) Immutability, Anonymous Type, and Tuple
Immutability is an important aspect of functional paradigm. As fore mentioned, imperative/object-oriented programming is usually stateful, and functional programming encourages immutability without state change. In C# programming, there are many kinds of immutability, but they can be categorized into 2 levels: immutability of some value, and immutability of some value’s internal state. Take local variable as example, a local variable can be called immutable, if once it is assigned, there is no way to reassign to it; a local variable can also be called immutable, if once its internal state is initialized, there is no way to modify its state to different state.
-
C# Functional Programming In-Depth (11) Covariance and Contravariance
In covariance and contravariance, variance means the capability to substitute a type with a more derived type or less derived type in a context. The following is a simple inheritance hierarchy:
-
C# Functional Programming In-Depth (10) Query Expression
C# 3.0 introduces query expression, a SQL-like query syntactic sugar for query methods composition.
-
C# Functional Programming In-Depth (9) Function Composition and Chaining
In object-oriented programming, objects can be composed to build more complex object. Similarly, in functional programming. functions can be composed to build more complex function.
-
C# Functional Programming In-Depth (8) Higher-order Function, Currying and First Class Function
Higher-order function is a function accepting one or more function parameters as input, or returning a function as output. The other functions are called first-order functions. C# supports higher-order function from the beginning. Generally, C# function can have almost any data type and function type as its input types and output type, except:
-
C# Functional Programming In-Depth (7) Expression Tree: Function as Data
C# lambda expression is a powerful syntactic sugar. Besides representing anonymous function, the same syntax can also represent expression tree.
-
C# Functional Programming In-Depth (6) Anonymous Function and Lambda Expression
Besides named function represented by method members, C# also supports anonymous functions, represented by anonymous method or lambda expression with no name at design time. This part discussed lambda expression as a functional feature of C# language. In the meanwhile, the general concept of lambda expression is the core of lambda calculus, where functional programming originates. General lambda expression and lambda calculus will be discussed in the Lambda Calculus chapter.
-
C# functional programming in-depth (5) Delegate: Function type, instance and group
In C#, functions are represented by methods of types, and other function members of types. In C#, just like just objects have types, methods/functions have types too, which are represented by delegate type.
-
C# functional programming in-depth (4) Function input and output
In C#, by default, arguments are passed to parameters by value. In the following example, the PassByValue function has a Uri parameter and a int type parameter. Uri is class so it is reference type, and int is structure so it is value type:
-
C# functional programming in-depth (3) Local Function and Closure
C# 7.0 introduces local function, which allows defining and calling a named, inline function inside a function member’s body. Unlike a local variable, which has to be used after being defined, a local function can be called before or after it is defined:
-
C# functional programming in-depth (2) Named function and function polymorphism
In C#, the most intuitive functions are method members of class and structure, including static method, instance method, and extension method, etc. These methods have names at design and are called by name, so they are named functions. Some other method-like members, including static constructor, constructor, finalizer, conversion operator, operator overload, property, indexer, event accessor, are also named functions, with specific name generated by at compiled time. This chapter discusses named functions in C#, how these named functions are defined, and looks into how they work. Method member’s name is available at design time, which some other function members’ name are generated at compile time.
-
C# functional programming in-depth (1) C# language fundamentals
C# 1.0 was initially released in 2002, as its first language specification says at the beginning, C# is a “simple, modern, object oriented, and type-safe” programming language for general purpose. Now C# has evolved to 7.2. During the years, a lot of great language features, especially rich functional programming features, has been added to C#. Now C# language has been productive and elegant, imperative and declarative, object-oriented and functional. With frameworks like .NET Framework, .NET Core, Mono, Xamarin, Unity, etc., C# is used by millions of people cross different platforms, including Windows, Linux, Mac, iOS, Android, etc.