Home » F# Reference Cell

F# Reference Cell

by Online Tutorials Library

F# Reference Cells

Reference cells refer to memory locations. It allows you to create mutable values. F# uses immutable data structure by default.

You can create reference cells by using ref operator. It holds actual value.

Reference cells can be dereferenced by using! (Bang) operator. It uses : = operator to assign new value.

Syntax:


F# Reference Cells Example

Output:

50 

F# Reference Cells Example2

Output:

50 100 

F# Reference Cells Value Property and Contents Field Example

Output:

101 102 

F# Reference Cells vs. Mutable Variables

Reference cells and mutable variables both can be used in all situations except that compiler does not allow to use mutable variables in lambda expressions, sequence expressions etc. In such scenario, you can use reference cells.

Next TopicF# Structures

You may also like