Прочетете на английски Редактиране

Споделяне чрез


SessionStateItemCollection.Item[] Property

Definition

Gets or sets a value in the collection.

Overloads

Item[Int32]

Gets or sets a value in the collection by numerical index.

Item[String]

Gets or sets a value in the collection by name.

Item[Int32]

Gets or sets a value in the collection by numerical index.

C#
public object this[int index] { get; set; }

Parameters

index
Int32

The numerical index of the value in the collection.

Property Value

The value in the collection stored at the specified index. If the specified key is not found, attempting to get it returns null, and attempting to set it creates a new element using the specified key.

Implements

Examples

Важно

Using an instance of this object with untrusted data is a security risk. Use this object only with trusted data. For more information, see Validate All Inputs.

The following code example sets and gets values in a SessionStateItemCollection collection by numerical index.

C#
SessionStateItemCollection sessionItems = new SessionStateItemCollection();

sessionItems["ZipCode"] = "98072";
sessionItems["Email"] = "someone@example.com";

for (int i = 0; i < items.Count; i++)
  Response.Write("sessionItems[" + i + "] = " + sessionItems[i].ToString() + "<br />");

See also

Applies to

.NET Framework 4.8.1 и други версии
Продукт Версии
.NET Framework 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

Item[String]

Gets or sets a value in the collection by name.

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

Parameters

name
String

The key name of the value in the collection.

Property Value

The value in the collection with the specified name. If the specified key is not found, attempting to get it returns null, and attempting to set it creates a new element using the specified key.

Implements

Examples

Важно

Using an instance of this object with untrusted data is a security risk. Use this object only with trusted data. For more information, see Validate All Inputs.

The following code example sets and gets values in a SessionStateItemCollection collection by name.

C#
SessionStateItemCollection items = new SessionStateItemCollection();

items["LastName"] = "Wilson";
items["FirstName"] = "Dan";

foreach (string s in items.Keys)
  Response.Write("items[\"" + s + "\"] = " + items[s].ToString() + "<br />");

See also

Applies to

.NET Framework 4.8.1 и други версии
Продукт Версии
.NET Framework 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