Cache.Item[String] 屬性

定義

取得或設定在指定索引鍵的快取項目。

public:
 property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ key); void set(System::String ^ key, System::Object ^ value); };
public object this[string key] { get; set; }
member this.Item(string) : obj with get, set
Default Public Property Item(key As String) As Object

參數

key
String

String 物件,表示快取項目的索引鍵。

屬性值

指定的快取項目。

範例

下列範例會 Item 使用 屬性來擷取與索引鍵相關聯的 Key1 快取物件值。 然後, HttpResponse.Write 它會使用 方法,將值和簡介文字和 B HTML 元素寫入至Web Form頁面。

Response.Write("Value of cache key: <B>" + Server.HtmlEncode(Cache["Key1"] as string) + "</B>");
Response.Write("Value of cache key: <B>" + Server.HtmlEncode(CType(Cache("Key1"),String)) + "</B>")

下列範例示範如何使用這個屬性,將文字方塊的值插入快取中。

private void cmdAdd_Click(Object objSender, EventArgs objArgs)
{
    if (txtName.Text != "")
    {
        // Add this item to the cache.
        Cache[txtName.Text] = txtValue.Text;
    }
}

Private Sub cmdAdd_Click(objSender As Object, objArgs As EventArgs)
  If txtName.Text <> "" Then
    ' Add this item to the cache.
  Cache(txtName.Text) = txtValue.Text
  End If
End Sub

備註

您可以使用這個屬性來擷取指定快取專案的值,或將專案和索引鍵新增至快取。 使用 Item[] 屬性新增快取專案相當於呼叫 Cache.Insert 方法。

適用於

另請參閱