处置名单的最佳方法 [英] Best way to dispose a list

查看:135
本文介绍了处置名单的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有列表对象。我该如何处置名单?

I am having List object. How I can dispose the list ?

例如,

List<User> usersCollection =new List<User>();

User user1 = new User();
User user2 = new User()

userCollection.Add(user1);
userCollection.Add(user2);

如果我设置 userCollection = NULL; 会发生什么

foreach(User user in userCollection)
{
    user = null;
}

哪一个是最好的?

Which one is best?

推荐答案

最好的办法是把它留给垃圾回收器。
你的的foreach 不会做任何事情,因为只有参考将被设置为不在列表中的元素。列表设置为可能在以后发生事实上的事业垃圾收集比它可能有(看到这个帖子的 C#:?应该对象变量被分配到空

Best idea is to leave it to the garbage collector. Your foreach will do nothing since only the reference will be set to null not the element in the list. Setting the list to null could in fact cause garbage collection to occur later than it could have (see this post C#: should object variables be assigned to null?).

这篇关于处置名单的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆