将ListView的Object指定为Tag属性 [英] Assigning Object to Tag property of ListView

查看:74
本文介绍了将ListView的Object指定为Tag属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码..



I have a code like this..

class ABC
{
   public string Name, Phone; 

   public static IEnumerable<ABC> GetList()
   {
      List<ABC> foo = new List<ABC>();

      //Some COde here
      return foo; 
   }
}

Class A
{
   private void GetList()
   {
      IEnumerable<ABC> abc = ABC.GetList();
      foreach (ABC newitem in abc)
      {
         ListViewItem li = new ListViewItem();
         li.Text = newitem.Name;
         li.Tag = newitem; //<<<<<<<<<<<<< Is this Possible ??? 
         ListView1.Items.Add(li);
      }
   }
}







我想知道如果我可以将一个对象分配给ListViewItem的Tag属性??

为什么它可以在外面访问,因为该对象位于一个函数内?

它会导致任何问题在未来 ??内存等...... :(请Helpp :(




I want to know If I can assign an object to the Tag Property of ListViewItem ??
How come it be accessible outside since the object resides inside a function ??
Will it cause any problems in future ?? Memory and so.... :( Please Helpp :(

推荐答案

是的,这就是Tag属性的用途。



该对象可以在函数外部访问,因为您正在为它分配一个引用。由于有一个对它的引用,它不是垃圾收集的,它存在于分配它的函数之外。



除非你在对象上使用事件,否则它将来不会引起任何问题。然后你需要记住在将对象赋值为null之前释放事件以便垃圾收集器将它拿起并销毁它。仅仅像上面那样使用它作为参考不会引起任何问题,我经常这样做。
Yes, that's what the Tag property is there for.

The object is accessible outside the function because you are assigning a reference to it. Since there is a reference to it, it's not garbage collected and it exists outside the function where it is assigned.

It should not cause any problems in the future unless you use events on the object. Then you would need to remember to release the events before assigning the object to null so that the garbage collector will pick it up and destroy it. Simply using it as a reference like you do above will not cause any issues, I do it quite frequently.


这篇关于将ListView的Object指定为Tag属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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