Understanding F# Records
The purpose of this article is to illustrate how F# records can be defined and used. Here, we are going to talk about the syntax that is required for defining records, accessing their fields, and creating instances of records.
Moreover, we will talk about the advantages of using F# records, such as the fact that they are immutable and that they support pattern matching, as well.
F# Records
F# record is similar to a class or struct in another programming language, but there are a few key differences that make them special from other programming languages.
Record values are immutable by default, which means that once they have been created, they cannot be changed.
A record type can be considered as another value type, so it is similar to a reference type in that it is passed as a value instead of a reference to another record.
Syntax
type website = { title : string; url : string }
Create a F# Record
In order to create a record, you need to specify the fields of the record.
Suppose that we were to create a record for the website called home, for example:
let homepage = { Title = "mrexamples"; Url = "www.mrexamples.com" }
Below is an example that clearly illustrates the concept of records.
Declare Record In F#
There are two types of records defined, namely:
- Records that are defined by the type keyword.
- Records that contain fields.
Both are defined by a semicolon-separated list of fields.
The syntax for defining a record is as follows:
type recordName = { [ fieldName : dataType ] + }
Defining A Record Type Named Website
Below is an example of defining records of the website name and their urls:
Example: 
In the example above, the following records are printed:
Home: Title: mrexample URL: https://mrexamples.com/ Learn F# from mrexamples: Title: Learn F# URL: https://mrexamples.com/f-sharp/ Learn Python from mrexamples: Title: Learn Python URL: https://mrexamples.com/python/ Learn Python from mrexamples: Title: Learn PHP URL: https://mrexamples.com/php/
Another example of creating a record about a pet:
Example: 
The output will be as:
Example Explanation
In above example we have define a record type named Pet with four fields: Name of type string, Age of type int, Species of type string, and Owner of type string.
Then we creates an instance of the Pet record named myPet with the values “Cute” for the name, 2 for the age, “Dog” for the species, and “Christopher” for the owner.
Next, we used the printfn function to print out the name and age of myPet record to the console.
Finally, we define a function named printPetAge that takes a Pet record as input and uses pattern matching to extract the Age field and print it to the console. If the Age field is not present in the input record, the function prints “Unable to determine pet’s age“.
The printPetAge function is called with myPet record as an argument, and it prints The pet’s age is 2 to the console.
Student Registration Records
Below is an example of registering student record:
Example: 
The results of both before and after registration and will be displayed as follows:
Before Registration: : {Name = Jennifer; ID = 105; Registration = null; IsRegistered = false} After Registration: : {Name = Jennifer; ID = 105; Registration = Registered; IsRegistered = true}