SortedList.System.Collections.IDictionary.Item プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > SortedList.System.Collections.IDictionary.Item プロパティの意味・解説 

SortedList.System.Collections.IDictionary.Item プロパティ

メモ : このプロパティは、.NET Framework version 2.0新しく追加されたものです。

指定したキー持つ要素取得または設定します

名前空間: System.Collections.Generic
アセンブリ: System (system.dll 内)
構文構文

Private Property System.Collections.IDictionary.Item
 ( _
    key As Object _
) As Object Implements IDictionary.Item
Dim instance As SortedList(Of
 TKey, TValue)
Dim key As Object
Dim value As Object

value = CType(instance, IDictionary).Item(key)

CType(instance, IDictionary).Item(key) = value
Object IDictionary.this [
    Object key
] { get; set; }
private:
virtual property Object^ System.Collections.IDictionary.Item [Object^] {
    Object^ get (Object^ key) sealed = IDictionary::default::get;
    void set (Object^ key, Object^ value) sealed
 = IDictionary::default::set;
}

パラメータ

key

取得または設定する要素キー

プロパティ
key がディクショナリにない場合、または key が SortedList のキーTKey代入できる型ではない場合は、指定したキー関連付けられた要素または null 参照 (Visual Basic では Nothing)。

例外例外
例外種類条件

ArgumentNullException

keynull 参照 (Visual Basic では Nothing) です。

ArgumentException

値を代入ようとしていますが、key は、SortedListキーの型 TKey代入できない型です。

または

値を代入ようとしていますが、value は、SortedList の値の型 TValue代入できない型です。

解説解説

このプロパティは、key が、SortedListキーの型 TKey代入できない型である場合に、null 参照 (Visual Basic では Nothing) を返します

このプロパティでは、myCollection[key] という構文使用してコレクション内の特定の要素アクセスできます

Item プロパティ使用すると、ディクショナリ内に存在しないキーの値を設定することで、新し要素追加することもできます (例 : myCollection["myNonexistentKey"] = myValue)。ただし、指定したキーがディクショナリに既に存在する場合Item プロパティ設定すると、既存の値が上書きされます対照的にAdd メソッド既存要素変更しません。

このプロパティ値を取得することは、O(log n) 操作なります。ここで、n は、Count です。キーが既に SortedList存在する場合、このプロパティ設定することは、O(log n) 操作なりますキーリスト存在しない場合、このプロパティ設定することは、並べ替えられていないデータ対する O(n) 操作なりますまた、新し要素リスト末尾追加される場合は、O(log n) 操作なります挿入によってサイズ変更発生する場合操作は O(n) になります

使用例使用例

System.Collections.IDictionary インターフェイスSystem.Collections.IDictionary.Item プロパティ (C# の場合インデクサ) を SortedList使用する方法、およびこのプロパティと SortedList.Item プロパティとの違い次のコード例示します

このコード例では、SortedList.Item プロパティ同様、SortedList.System.Collections.IDictionary.Item プロパティ使用して既存キー関連付けられた値を変更したり、指定されキー並べ替えられたリストない場合新しキー/値ペア追加したできること示しますまた、SortedList.Item プロパティ異なりSortedList.System.Collections.IDictionary.Item プロパティkey並べ替えられたリストない場合例外スローしないで代わりに null 参照返すことを示します最後にSortedList.System.Collections.IDictionary.Item プロパティ取得してkey適切なデータ型ではない場合null 参照返す例や、プロパティ設定してkey適切なデータ型ない場合例外返す例も示します

次のコード例は、System.Collections.IDictionary.Add メソッドトピック取り上げている例および出力一部分です。

Imports System
Imports System.Collections
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main()
 

        ' Create a new sorted list of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New sortedList(Of String,
 String)
        
        ' Add some elements to the sorted list. There are no 
        ' duplicate keys, but some of the values are duplicates.
        ' IDictionary.Add throws an exception if incorrect types
        ' are supplied for key or value.
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
<br /><span space="preserve">...</span><br
 />        ' The Item property is the default property, so you 
        ' can omit its name when accessing elements. 
        Console.WriteLine("For key = ""rtf"",
 value = {0}.", _
            openWith("rtf"))
        
        ' The default Item property can be used to change the value
        ' associated with a key.
        openWith("rtf") = "winword.exe"
        Console.WriteLine("For key = ""rtf"",
 value = {0}.", _
            openWith("rtf"))
        
        ' If a key does not exist, setting the default Item property
        ' for that key adds a new key/value pair.
        openWith("doc") = "winword.exe"

        ' The default Item property returns Nothing if the key
        ' is of the wrong data type.
        Console.WriteLine("The default Item property returns Nothing"
 _
            & " if the key is of the wrong type:")
        Console.WriteLine("For key = 2, value = {0}.",
 _
            openWith(2))

        ' The default Item property throws an exception when setting
        ' a value if the key is of the wrong data type.
        Try
            openWith(2) = "This does not get added."
        Catch 
            Console.WriteLine("A key of the wrong type was specified"
 _
                & " when setting the default Item property.")
        End Try
<br /><span space="preserve">...</span><br
 />        ' Unlike the default Item property on the SortedList
 class
        ' itself, IDictionary.Item does not throw an exception
        ' if the requested key is not in the sorted list.
        Console.WriteLine("For key = ""tif"",
 value = {0}.", _
            openWith("tif"))
<br /><span space="preserve">...</span><br
 />
    End Sub

End Class
using System;
using System.Collections;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new sorted list of strings, with string keys,
        // and access it using the IDictionary interface.
        //
        IDictionary openWith = new SortedList<string,
 string>();

        // Add some elements to the sorted list. There are no 
        // duplicate keys, but some of the values are duplicates.
        // IDictionary.Add throws an exception if incorrect types
        // are supplied for key or value.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");
<br /><span space="preserve">...</span><br /> 
       // The Item property is another name for the indexer, so you
 
        // can omit its name when accessing elements. 
        Console.WriteLine("For key = \"rtf\", value = {0}.",
 
            openWith["rtf"]);

        // The indexer can be used to change the value associated
        // with a key.
        openWith["rtf"] = "winword.exe";
        Console.WriteLine("For key = \"rtf\", value = {0}.",
 
            openWith["rtf"]);

        // If a key does not exist, setting the indexer for that key
        // adds a new key/value pair.
        openWith["doc"] = "winword.exe";

        // The indexer returns null if the key is of the wrong data
 
        // type.
        Console.WriteLine("The indexer returns null"
 
            + " if the key is of the wrong type:");
        Console.WriteLine("For key = 2, value = {0}.", 
            openWith[2]);

        // The indexer throws an exception when setting a value
        // if the key is of the wrong data type.
        try
        {
            openWith[2] = "This does not get added.";
        }
        catch (ArgumentException)
        {
            Console.WriteLine("A key of the wrong type was specified" 
                + " when assigning to the indexer.");
        }
<br /><span space="preserve">...</span><br /> 
       // Unlike the default Item property on the SorteList class
        // itself, IDictionary.Item does not throw an exception
        // if the requested key is not in the sorted list.
        Console.WriteLine("For key = \"tif\", value = {0}.",
 
            openWith["tif"]);
<br /><span space="preserve">...</span><br /> 
   }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SortedList ジェネリック クラス
SortedList メンバ
System.Collections.Generic 名前空間
SortedList.Item プロパティ
Add


このページでは「.NET Framework クラス ライブラリ リファレンス」からSortedList.System.Collections.IDictionary.Item プロパティを検索した結果を表示しています。
Weblioに収録されているすべての辞書からSortedList.System.Collections.IDictionary.Item プロパティを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からSortedList.System.Collections.IDictionary.Item プロパティを検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

SortedList.System.Collections.IDictionary.Item プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



SortedList.System.Collections.IDictionary.Item プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS