Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, what would be the closest equivalent F# code to a C# array of objects?

Example:

In C# I would create:

C#
object[,] tableA = new object[30,2];

C#
tableA [0, 0] = "dog";
tableA [0, 1] = 2;
tableA [1, 0] = "cat";
tableA [1, 1] = 31;


and so on ....


In F# I could create

fs
let tableA_FS = Map( [("dog", 2); ("cat", 31)] );


However I cannot cast a map into a type object[,]



what is the closest type in F# to obj[,]?
Posted

In this particular case, you can use array2D:
http://msdn.microsoft.com/en-us/library/ee620670.aspx[^].

See also the section "Multidimensional Arrays" in the documentation page on F# arrays:
http://msdn.microsoft.com/en-us/library/dd233214.aspx[^].

—SA
 
Share this answer
 
Comments
tagad 2-Jul-12 14:31pm    
Hi SA,

thanks for your reply. An array2D can be specified in 1 type. In my example I am using string and integer as types. I am not clear from the reference you pointed how to initialize an array2D in 2 different types.


thanks
Sergey Alexandrovich Kryukov 2-Jul-12 15:47pm    
No, you are not using different types! You use one polymorphic type System.Object. It has nothing to do with the array rank, could be in single-dimensional array in exact same way. You can do exactly the same thing in F#.
--SA
A possible way to do it is

F#
let myArray2D : obj[,] = Array2D.create 2 2 (box())
myArray2D.[0,0] <- box("cc")
myArray2D.[0,1] <- box(2.0)
...


this has type obj[,]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900