Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# equivalent of the C# 'object' keyword

Tags:

c#

f#

c#-to-f#

I am trying to port an existing c# to f# and would I am getting stuck on porting this c# line:

public object myInnerOject
like image 956
akaphenom Avatar asked May 18 '10 00:05

akaphenom


2 Answers

In C# object is just a type name referring to System.Object. In F#, you can either use this full .NET name or a type alias obj.

Now, regarding a field declaration - there are various ways to write that depending on the context. Also, it is a good practice to initialize all fields in F#, so it would be useful to see larger context of your code.

However, you may write this:

type A() = 
  let mutable (myInnerObj:obj) = null

This creates a private field myInnerObj of type System.Object initialized to null, which is mutable can can be assigned later (myInnerObj <- new Random(), for example). I used example with private field, because public fields are generally discouraged in .NET.

like image 50
Tomas Petricek Avatar answered Oct 10 '22 09:10

Tomas Petricek


@Tomas is right. You may also want to check out these blogs:

The basic syntax of F# - types

What does this C# code look like in F#?

like image 35
Brian Avatar answered Oct 10 '22 09:10

Brian