Virtual methods and overrides

 In F#, the abstract keyword is used to declare a virtual member. So, here we can write a complete definition of the member as we use abstract for virtual. F# is not similar to other .NET languages. Let's have a look at the following example:

type MyClassExampleBase() =   let mutable x = 0   abstract member virtualMethodExample : int -> int   default u. virtualMethodExample (a : int) = x <- x + a; xtype MyClassExampleDerived() =   inherit MyClassExampleBase ()   override u. virtualMethodExample (a: int) = a + 1

In the previous example, we declared a virtual method, virtualMethodExample, in a base class, MyClassExampleBase, and overrode it in a derived class, MyClassExampleDerived.

Get .NET Core 2.0 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.