Below is my code
private List<string> _myList
public List<string> myList
{
get
{
if (Session["MyData"] != null)
_myList = Session["MyData"] as List<string>;
if(_myList==null)
_myList = new List<string>();
return _myList;
}
set
{
Session["MyData"] = value;
}
}
Now when I call
myList.add(new string("string1"));
and use
myList.Count
I am getting myList.Count equals 0 I don't know what is the problem with my code.
this overrides the list:
get
{
if (Session["MyData"] != null)
_myList = Session["MyData"] as List<string>; //<-- here
if(_myList==null)
_myList = new List<string>();
return _myList;
}
Try changing it to:
get
{
if(_myList != null)
return _mylist;
if (Session["MyData"] != null)
_myList = Session["MyData"] as List<string>;
else
_myList = new List<string>();
return _myList;
}
You are not modifying the _myList. In your get, sometimes you return a new List<string> so you Add the new item to that new List and when calling it again you count the member for another new List<string>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With