What use is the Tag property in .net

后端 未结 4 1750
暗喜
暗喜 2020-12-09 04:13

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 unboxin

4条回答
  •  生来不讨喜
    2020-12-09 04:30

    Well, you could create your own TreeNode derived class:

    class MyNode : TreeNode {
        public int number {get; set;}
    }
    

    But then you'll be casting when you retrieve the node from the tree, no improvement over casting the Tag property. And you ought to override the Clone() method.

    A cleaner approach is to leverage TreeNode.Name and make that a key in a dictionary to find your custom data back. Good when CustClass gets to be non-trivial. The Name property isn't used for anything else.

提交回复
热议问题