Cache.Item[String] Property

Definition

Gets or sets the cache item at the specified key.

C#
public object this[string key] { get; set; }

Parameters

key
String

A String object that represents the key for the cache item.

Property Value

The specified cache item.

Examples

The following example uses the Item property to retrieve the value of a cached object associated with the Key1 key. It then uses the HttpResponse.Write method to write the value and introductory text and the B HTML element to a Web Forms page.

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

The following example demonstrates using this property to insert the value of a text box into the cache.

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

Remarks

You can use this property to retrieve the value of a specified cache item, or to add an item and a key for it to the cache. Adding a cache item using the Item[] property is equivalent to calling the Cache.Insert method.

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also