How do I cast a asp net session to get back my list


ASP.NET is a popular programming language used for developing web applications. One task in ASP.NET is to and retrieve data from session variables. In this article, we will discuss how to cast an ASP.NET session to get back a list.

Understanding ASP.NET Session

ASP.NET session is a mechanism that allows you to store user-specific data on the server. It provides a way to persist data across multiple from the same user. Session variables are stored in memory and can be accessed throughout the user's session.

Storing a List in ASP.NET Session

Before we can retrieve a list from the session, we need to store it first. Let's assume we have a list of that we want to store in the session:

List myList = new List();
myList.Add("Item 1");
myList.Add("Item 2");
myList.Add("Item 3");

Session["MyList"] = myList;

In the above code, we a new list and add some items to it. Then, we store the list in the session using the key “MyList”. Now, let's see how we can retrieve this list from the session.

Retrieving a List from ASP.NET Session

To retrieve a list from the session, we need to cast the session variable to the appropriate type. In this case, we want to cast it to a list of strings:

List myList = Session["MyList"] as List;

if (myList != null)
    // Use the list
    foreach (string item in myList)

In the above code, we use the “as” to cast the session variable to a list of strings. If the cast is , we can use the list as desired. In this example, we over the list and each item.


In this article, we discussed how to cast an ASP.NET session to get back a list. We learned how to store a list in the session and retrieve it using the appropriate cast. By understanding this concept, you can effectively use session variables to store and retrieve data in your ASP.NET applications.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents