什么用途是在.NET中的Tag属性 [英] What use is the Tag property in .net

查看:77
本文介绍了什么用途是在.NET中的Tag属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注意到了标签性能与控制。它是利用好这个引用我的自定义对象,或者我应该远离它,因为它需要装箱和拆箱已经提到不安全的,并且不推荐。

 树节点TN =新的TreeNode();
CustClass O =新CustClass()
o.number = 123;
tn.Tag = O;

类CustClass
{
    公众诠释号{获得;组;}
}
 

解决方案

标签的目的属性是您可以使用它为任何你想要的目的。在那里,你想,你可以安全地存储任何东西。

这是习惯来声明一个类是专门用于存储在标签属性(比如你做你的 CustClass )。但是,如果你需要它只有一个值,那么就没有什么不妥存储一个 INT 的直接。

你在哪里看,拳击/拆箱是不安全?这是绝对不正确的。 (有些人声称,这是的低效的,但即使事实并非如此)。此外,你的code甚至不包含所有拳击的一个例子。 CustClass 是引用类型。当分配给对象只值类型盒装

I have noticed the Tag properties with controls. Is it okay to use this to reference my custom objects, or should I stay away from it as it would require boxing and unboxing which has been mentioned as unsafe and is not recomended.

TreeNode tn = new TreeNode ();
CustClass o = new CustClass()
o.number = 123;
tn.Tag = o;

class CustClass
{
    public int number {get; set;}
}

解决方案

The purpose of the Tag property is for you to use it for any purpose you want. You can safely store anything in there you want.

It is customary to declare a class that is specifically intended for being stored in the Tag property (like you did with your CustClass). But if you need only one value in it, then there is nothing wrong with storing an int in it directly.

Where did you read that boxing/unboxing is "unsafe"? That is absolutely not true. (Some people claim that it is inefficient, but even that is not true.) Furthermore, your code does not even contain an example of boxing at all. CustClass is a reference type. Only value types are boxed when assigned to object.

这篇关于什么用途是在.NET中的Tag属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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