No results found for the specified position. ASP.net - Differentiate between a session ... C# Intermediate Mock Interview

MockQuestions

C# Intermediate Mock Interview

Question 2 of 20 for our C# Intermediate Mock Interview

Get More Information About Our C# Intermediate Interview Questions

Question 2 of 20

ASP.net - Differentiate between a session object and a cache object

This question is looking for a developer's understanding of how data is stored.

As a web developer, you'll encounter situations where you need to persist data relevant to the current user session or when that data is relevant for the entire application or is global. The session object persists only for that current user session goes away when the session object times out or is explicitly destroyed. You can define a session object in your code as follows:

Order currentOrder = new Order();
Item itm = new Item();
currentOrder.AddItem(itm);
Session["order'] = currentOrder;

// perform other actions with the order
// when you're done, you can cancel the session as follows:

Session.Abandon();

/** Persisting data or an object(s) for the lifetime of an application may be relevant when you want to share data across multiple sessions.  In this case, the Cache object would be appropriate. **/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace WebApplication1
{
    public partial class _Default : Page
    {

        List<string> str = new List<string>();        

        protected void Page_Load(object sender, EventArgs e)
        {
            
           if (Cache["ListOfStrings"] == null){
                str.Add("One");
                str.Add("Two");
                str.Add("Three");

                /** Insert() is an overloaded method, so you can add other parameters to define the lifetime of the cache object how the cache expires and cache dependencies.  The ListOfStrings Cache object is now available to the entire application and multiple user sessions. **/

                Cache.Insert("ListOfStrings", str);

            }
           else
            {
                // if it has already been created and populated, you can now do something with the Cache object
                List<string> stringList = (List<string>)Cache["ListOfStrings"];

                foreach (string str in stringList)
                {
                    //Perform some tasks based upon the string in the list

                }

            }
        }
    }
}

Written by on May 6th, 2021

Next Question

How to Answer: ASP.net - Differentiate between a session object and a cache object

Advice and answer examples written specifically for a C# Intermediate job interview.

  • 2. ASP.net - Differentiate between a session object and a cache object

      This question is looking for a developer's understanding of how data is stored.

      As a web developer, you'll encounter situations where you need to persist data relevant to the current user session or when that data is relevant for the entire application or is global. The session object persists only for that current user session goes away when the session object times out or is explicitly destroyed. You can define a session object in your code as follows:

      Order currentOrder = new Order();
      Item itm = new Item();
      currentOrder.AddItem(itm);
      Session["order'] = currentOrder;
      
      // perform other actions with the order
      // when you're done, you can cancel the session as follows:
      
      Session.Abandon();
      
      /** Persisting data or an object(s) for the lifetime of an application may be relevant when you want to share data across multiple sessions.  In this case, the Cache object would be appropriate. **/
      
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      
      
      namespace WebApplication1
      {
          public partial class _Default : Page
          {
      
              List<string> str = new List<string>();        
      
              protected void Page_Load(object sender, EventArgs e)
              {
                  
                 if (Cache["ListOfStrings"] == null){
                      str.Add("One");
                      str.Add("Two");
                      str.Add("Three");
      
                      /** Insert() is an overloaded method, so you can add other parameters to define the lifetime of the cache object how the cache expires and cache dependencies.  The ListOfStrings Cache object is now available to the entire application and multiple user sessions. **/
      
                      Cache.Insert("ListOfStrings", str);
      
                  }
                 else
                  {
                      // if it has already been created and populated, you can now do something with the Cache object
                      List<string> stringList = (List<string>)Cache["ListOfStrings"];
      
                      foreach (string str in stringList)
                      {
                          //Perform some tasks based upon the string in the list
      
                      }
      
                  }
              }
          }
      }

      Written by Edward Danganan on May 3rd, 2021