A structure is a compact object type that can be more efficient than a class for types with a small amount of data and simple behavior.
You can define structures in two different ways, as shown below.
[ attributes ]
type [accessibility-modifier] type-name =
struct
type-definition-elements-and-members
end
// or
[ attributes ]
[<StructAttribute>]
type [accessibility-modifier] type-name =
type-definition-elements-and-members
StructAttribute
attribute, which appears in the second form.StructAttribute
to just Struct.The following example demonstrates the usage of a structure.
type Rectangle =
struct
val Height: float
val Width: float
new(x: float, y: float) =
{ Height = x; Width = y }
member this.GetArea() = this.Height * this.Width
end
let rectangle = new Rectangle(3.0, 5.0)
Console.WriteLine("Area: {0}", rectangle.GetArea())
When you execute the above code, it will display the following output on the console.
Area: 15