3
Answers

Use of Get Set property with Session Sate.

Hi, I have recently started on C# and getting doubts. Can anyone help me out in understanding the below code, specialy the importance of get accessor with session. Any help will be appreciated. Thanks.
 
 
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
public static class ExampleSession
{
private static HttpSessionState session { get { return HttpContext.Current.Session; } }
public static string UserName
{
get { return session["username"] as string; }
set { session["username"] = value; }
}
public static List<string> ProductsSelected
{
get
{
if (session["products_selected"] == null)
session["products_selected"] = new List<string>();
return (List<string>)session["products_selected"];
}
}
}
 
Answers (3)
1
Manav Pandya

Manav Pandya

NA 7.1k 24k 7y
Hello 
 
Get : It is used when you get the value of an variable
 
Set : It is used when you assign a value o variable or objects
 
For Example :
 
When session being check for either null or not ,its call get request
else set request when we define value like session["ID"] = 1 ;
 
Thanks 
Accepted
1
Manav Pandya

Manav Pandya

NA 7.1k 24k 7y
Hello 
 
Always welcome brother @saurabh 
1
saurabh choudhary

saurabh choudhary

NA 12 152 7y
Thank You Manav for your Time and the explaination.