Overview
.NET type classifications
Value types vs reference types
Primitive types vs composite types
Generic types
Types created from literal values
Algebraic types: sum vs product
Composite types
Created by combining other types
Can be generic (except
enum)
Types
Version
Name
Ref. type
Value type
Types .NET
class
β
β
struct, enum
β
β
Specific to Cβ―
Cβ― 3.0
Anonymous type
β
β
Cβ― 7.0
Value tuple
β
β
Cβ― 9.0
record (class)
β
β
Cβ― 10.0
record struct
β
β
Specific to Fβ―
Tuple, Record, Union
β (default)
β (opt-in)
Fβ― 4.6
Anonymous Record
β (default)
β (opt-in)
π Fβ― type features are stable and mature.
Type Location
Top-level :
namespace, top-levelmodule(Fβ―)Nested :
class(Cβ―),module(Fβ―)Not definable in
letbindings,member
In Fβ―, all type definitions are made with the type keyword
β including classes, enums and interfaces!
β but tuples don't need a type definition
Particularity of Fβ― types / .NET types
Tuple, Record, Union are:
Immutable by default
Non-nullable by default
Equality and structural comparison (except with fields of
functiontype)sealed: cannot be inheritedSupport deconstruction, with the same syntax than for construction
Types with literal values
Literal values = instances whose type is inferred
Primitive types:
true(bool) -"abc"(string) -1.0m(decimal)Tuples Cβ― / Fβ― :
(1, true)Anonymous types Cβ― :
new { Name = "Joe", Age = 18 }Records Fβ― :
{ Name = "Joe"; Age = 18 }
β Note :
Types must be defined beforehand β
Exception: tuples and Cβ― anonymous types: implicit definition
Algebraic data types (ADT)
Composite types, combining other types by product or sum.
Let's take the types A and B, then we can create:
The product type
A Γ B:Contains 1 component of type
AAND 1 of typeB.Anonymous or named components
Sum type
A + B:Contains 1 component of type
AOR 1 of typeB.
By extension, same for the product/sum types of N types.
Why Sum and Product terms?
It's related to the number of values:
boolβ 2 values:trueandfalseunitβ 1 value()intβ infinite number of values
The number of values in the composed type will be:
The sum of numbers for a sum type:
N(A) + N(B)The product of numbers for a product type:
N(A) * N(B)
Algebraic types vs Composite types
enum
β
β
Fβ― Union
β
β
Cβ― class (1), interface, struct
β
β
Fβ― Record
β
β
Fβ― Tuple
β
β
(1) Cβ― classes in the broadest sense: β including modern variations like anonymous type, Value tuple and Record
π In Cβ―, only 1 sum type: enum, very limited / union type π
Type abbreviation
Alias of another type: type [name] = [existingType]
Different use-cases:
β οΈ Deleted at compile time β no type safety
β Compiler allows int to be passed instead of CustomerId !
π‘ It is also possible to create an alias for a module π
module [name] = [existingModule]
β οΈ It's NOT possible to create an alias for a namespace (β Cβ―)
Last updated
Was this helpful?