Cache.Item Property (System.Web.Caching)

Switch View :
ScriptFree
.NET Framework Class Library
Cache.Item Property

Gets or sets the cache item at the specified key.

Namespace:  System.Web.Caching
Assembly:  System.Web (in System.Web.dll)
Syntax

Visual Basic
Public Property Item ( _
	key As String _
) As Object
	Get
	Set
C#
public Object this[
	string key
] { get; set; }
Visual C++
public:
property Object^ Item[String^ key] {
	Object^ get (String^ key);
	void set (String^ key, Object^ value);
}
F#
member Item : Object with get, set

Parameters

key
Type: System.String
A String object that represents the key for the cache item.

Property Value

Type: System.Object
The specified cache item.
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.

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.

Visual Basic

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


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.

Visual Basic

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


C#

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


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
See Also

Reference

Other Resources