我想在F#像這樣來創建一個標準的.NET List<T>
:F#中創建一個泛型列表<T>
module Csv
open System;
type Sheet() =
let rows = new List<Object>()
,但我得到了以下錯誤:
No constructors are available for the type
List<Object>
C:\…\Csv.fs
: 6
我在做什麼錯誤?
我想在F#像這樣來創建一個標準的.NET List<T>
:F#中創建一個泛型列表<T>
module Csv
open System;
type Sheet() =
let rows = new List<Object>()
,但我得到了以下錯誤:
No constructors are available for the type
List<Object>
C:\…\Csv.fs
: 6
我在做什麼錯誤?
至於別人提出一個簡單的替代,你可以使用一個名爲ResizeArray<T>
類型。這是System.Collections.Generic.List<T>
一個類型別名在F#核心庫定義:
type Sheet() =
let rows = new ResizeArray<Object>()
在編譯的代碼,ResizeArray<T>
會被編譯成System.Collections.Generic. List<T>
,因此,如果您使用的庫從C#,不會有任何區別。
您不必打開System.Collections.Generic
,這將隱藏F#List<T>
類型的定義(雖然這不是什麼大問題),我認爲這ResizeArray
是數據結構的更合適的名稱反正。
您需要打開System.Collections.Generic
,太 - 你引用List<_>
類型是F#的不可變列表類型(從Microsoft.FSharp.Collections
命名空間,這是默認開啓),它不公開公共構造函數。
的List<T>
class在System.Collections.Generic
namespace的,所以您需要添加:
open System.Collections.Generic