인덱싱된 속성(F#)

인덱싱된 속성은 순서가 정해진 데이터에 배열 형식으로 액세스할 수 있도록 하는 속성입니다.

// 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

설명

위에 나와 있는 세 가지 형식의 구문은 get 및 set 메서드가 둘 다 있거나 get 메서드만 있거나 set 메서드만 있는 인덱싱된 속성을 정의하는 방법을 보여 줍니다. get만 있는 속성의 구문과 set만 있는 속성의 구문을 결합하여 get과 set이 둘 다 있는 속성을 만들 수도 있습니다. 이 마지막 형식을 사용하면 get 메서드와 set 메서드에 대해 각기 다른 액세스 가능성 한정자와 특성을 지정할 수 있습니다.

PropertyName이 Item인 경우 컴파일러는 속성을 인덱싱된 기본 속성으로 취급합니다. 인덱싱된 기본 속성은 개체 인스턴스에 대해 배열 형식의 구문을 사용하여 액세스할 수 있는 속성입니다. 예를 들어 obj가 이 속성을 정의하는 형식의 개체인 경우 속성에 액세스하는 데는 obj.[index] 구문이 사용됩니다.

기본 속성이 아닌 인덱싱된 속성에 액세스하려면 속성의 이름을 지정하고 인덱스를 괄호로 묶어 표시하는 구문 형식을 사용해야 합니다. 예를 들어 속성이 Ordinal인 경우 이 속성에 액세스하려면 obj.Ordinal(index) 구문을 사용해야 합니다.

어떤 형식을 사용하든 상관없이 인덱싱된 속성에 대해서는 항상 set 메서드의 변환 형식을 사용해야 합니다. 변환 함수에 대한 자세한 내용은 함수(F#)를 참조하십시오.

예제

다음 코드 예제에서는 get 및 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

인덱스 변수가 여러 개인 인덱싱된 속성

인덱싱된 속성의 인덱스 변수는 여러 개일 수 있습니다. 인덱스 변수가 여러 개인 속성을 사용할 때는 각 변수를 쉼표로 구분합니다. 이와 같은 속성의 set 메서드에는 변환 인수 두 개가 있어야 합니다. 그중 첫째 인수는 키를 포함하는 튜플이고 둘째 인수는 설정할 값입니다.

다음 코드에서는 인덱스 변수가 여러 개인 인덱싱된 속성의 사용 방법을 보여 줍니다.

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

참고 항목

개념

멤버(F#)