Share via


Proprietà indicizzate (F#)

Le proprietà indicizzate sono proprietà che forniscono accesso di tipo matrice ai dati ordinati.

// Indexed property that has both get and set defined.
member self-identifier.PropertyName
   with get(index-variable) =
      get-function-body
  and set index-variables value-variables =
      set-function-body

// Indexed property that has get only.
member self-identifier.PropertyName(index-variable) =
      get-function-body

// Alternative syntax for indexed property with get only
member self-identifier.PropertyName
   with get(index-variables) =
      get-function-body

// Indexed property that has set only.
member self-identifier.PropertyName
   with set index-variables value-variables = 
      set-function-body

Note

Le tre forme della sintassi precedente mostrano come definire proprietà indicizzate che dispongono di entrambi i metodi get e set, che dispongono solo di un metodo get o che dispongono solo di un metodo set. È anche possibile combinare la sintassi illustrata solo per get e quella solo per set e produrre una proprietà che disponga sia di get che di set. Quest'ultima forma consente di applicare attributi e modificatori di accessibilità diversi ai metodi get e set.

Quando PropertyName è Item, la proprietà viene trattata dal compilatore come proprietà indicizzata predefinita. Una proprietà indicizzata predefinita è una proprietà a cui è possibile accedere utilizzando una sintassi di tipo matrice nell'istanza dell'oggetto. Se, ad esempio, obj è un oggetto del tipo che definisce la proprietà, la sintassi obj.[index] consente di accedere alla proprietà.

La sintassi per l'accesso a una proprietà indicizzata non predefinita prevede che vengano forniti il nome della proprietà e l'indice tra parentesi. Se, ad esempio, la proprietà è Ordinal, per accedervi è necessario scrivere obj.Ordinal(index).

Indipendentemente dalla forma utilizzata, è necessario utilizzare sempre la forma sottoposta a currying per il metodo set in una proprietà indicizzata. Per ulteriori informazioni sulle funzioni sottoposte a currying, vedere Funzioni (F#).

Esempio

Nell'esempio di codice seguente vengono illustrati la definizione e l'utilizzo di proprietà indicizzate predefinite e non predefinite che dispongono di metodi get e set.

type NumberStrings() =
   let mutable ordinals = [| "one"; "two"; "three"; "four"; "five";
                             "six"; "seven"; "eight"; "nine"; "ten" |]
   let mutable cardinals = [| "first"; "second"; "third"; "fourth";
                              "fifth"; "sixth"; "seventh"; "eighth";
                              "ninth"; "tenth" |]
   member this.Item
      with get(index) = ordinals.[index]
      and set index value = ordinals.[index] <- value
   member this.Ordinal
      with get(index) = ordinals.[index]
      and set index value = ordinals.[index] <- value
   member this.Cardinal
      with get(index) = cardinals.[index]
      and set index value = cardinals.[index] <- value

let nstrs = new NumberStrings()
nstrs.[0] <- "ONE" 
for i in 0 .. 9 do
  printf "%s " (nstrs.[i])
printfn ""

nstrs.Cardinal(5) <- "6th" 

for i in 0 .. 9 do
  printf "%s " (nstrs.Ordinal(i))
  printf "%s " (nstrs.Cardinal(i))
printfn ""

Output

ONE two three four five six seven eight nine ten
ONE first two second three third four fourth five fifth six 6th
seven seventh eight eighth nine ninth ten tenth

Proprietà indicizzate con più variabili di indice

Le proprietà indicizzate possono disporre di più di una variabile di indice. In tal caso, le variabili sono separate da virgole quando la proprietà viene utilizzata. Il metodo set in una proprietà di questo tipo deve disporre di due argomenti sottoposti a currying, il primo dei quali è una tupla contenente le chiavi e il secondo è il valore impostato.

Nel codice seguente viene illustrato l'utilizzo di una proprietà indicizzata con più variabili di indice.

open System.Collections.Generic

type SparseMatrix() =
    let mutable table = new Dictionary<(int * int), float>()
    member this.Item
        with get(key1, key2) = table.[(key1, key2)]
        and set (key1, key2) value = table.[(key1, key2)] <- value

let matrix1 = new SparseMatrix()
for i in 1..1000 do
    matrix1.[i, i] <- float i * float i

Vedere anche

Altre risorse

Membri (F#)