I am having List object. How can I dispose of the list?
For example,
List<User> usersCollection =new List<User>();
User user1 = new User();
User user2 = new User()
userCollection.Add(user1);
userCollection.Add(user2);
If I set userCollection = null; what will happen?
foreach(User user in userCollection)
{
user = null;
}
Which one is best?
Best idea is to leave it to the garbage collector.
Your
foreachwill do nothing since only the reference will be set tonullnot the element in the list. Setting the list tonullcould in fact cause garbage collection to occur later than it could have (see this post C#: should object variables be assigned to null?).