Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# Tag. When should I use it?

When I see my company working code, I sometimes see the code "Tag" member of Controls. My co-workers use it as a name tag. But the Controls have already a "Text", "Name" thingy properties. When I see the code, It looks fine to call the Controls with other properties.

I feel somewhat the "Tag" property is not for use of only giving the Control a specific name. So I looked up MSDN for a information about it.

They say "Tag" is an Object that can store any data like class. And get the data fast from the tag property. I want to know when is the case should a programmer use it for? Can anyone help me with an example situation and codes?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

By the way what are those attributes for? It seems those make the tag more special.

[System.ComponentModel.Bindable(true)]
[System.ComponentModel.TypeConverter(typeof(System.ComponentModel.StringConverter))]
public object Tag { get; set; }

>Solution :

The tag can be used for anything you like, common use cases

  • when a lot of controls share the same click handler
  • tag on list / tree items that let you associate data with the clicked object (a customer object in a list of customers say)

The name nor text is usable in the second case

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading