SoFunction
Updated on 2025-03-09

Class instance of C# operation session

This article describes the C# operation session class. Share it for your reference. The specific analysis is as follows:

This C# class re-encapsulates session operations, which can greatly simplify the common operations of sessions. At the same time, this class can set the session value to an array or read the value as an array list. If you have this need, you can use this class and expand it yourself to expand this C# class.

using ;
namespace 
{
 public static class SessionHelper2
 {
  /// <summary>
  /// Add Session, the transfer validity period is 20 minutes  /// </summary>
  /// <param name="strSessionName">Session object name</param>  /// <param name="strValue">Session value</param>  public static void Add(string strSessionName, string strValue)
  {
   [strSessionName] = strValue;
    = 20;
  }
  /// &lt;summary&gt;
  /// Add Session, the transfer validity period is 20 minutes  /// &lt;/summary&gt;
  /// <param name="strSessionName">Session object name</param>  /// <param name="strValues">Session value array</param>  public static void Adds(string strSessionName, string[] strValues)
  {
   [strSessionName] = strValues;
    = 20;
  }
  /// &lt;summary&gt;
  /// Add Session  /// &lt;/summary&gt;
  /// <param name="strSessionName">Session object name</param>  /// <param name="strValue">Session value</param>  /// <param name="iExpires">Mobilization validity period (minutes)</param>  public static void Add(string strSessionName, string strValue, int iExpires)
  {
   [strSessionName] = strValue;
    = iExpires;
  }
  /// &lt;summary&gt;
  /// Add Session  /// &lt;/summary&gt;
  /// <param name="strSessionName">Session object name</param>  /// <param name="strValues">Session value array</param>  /// <param name="iExpires">Mobilization validity period (minutes)</param>  public static void Adds(string strSessionName, string[] strValues, int iExpires)
  {
   [strSessionName] = strValues;
    = iExpires;
  }
  /// &lt;summary&gt;
  /// Read a Session object value  /// &lt;/summary&gt;
  /// <param name="strSessionName">Session object name</param>  /// <returns>Session object value</returns>  public static string Get(string strSessionName)
  {
   if ([strSessionName] == null)
   {
    return null;
   }
   else
   {
    return [strSessionName].ToString();
   }
  }
  /// &lt;summary&gt;
  /// Read an array of value of a Session object  /// &lt;/summary&gt;
  /// <param name="strSessionName">Session object name</param>  /// <returns>Session object value array</returns>  public static string[] Gets(string strSessionName)
  {
   if ([strSessionName] == null)
   {
    return null;
   }
   else
   {
    return (string[])[strSessionName];
   }
  }
  /// &lt;summary&gt;
  /// Delete a Session object  /// &lt;/summary&gt;
  /// <param name="strSessionName">Session object name</param>  public static void Del(string strSessionName)
  {
   [strSessionName] = null;
  }
 }
}

I hope this article will be helpful to everyone's C# programming.