For a program I’m making I’m using object to hold the value of HTML attributes. There’s a bunch if things I do with these values in the whole program. So I would basically like to create an alias for object whenever is used to hold values of HTML attributes to something like AttrValue just to make the program more clear, and to be able to easily add functionality if needed. These objects are used on critical-performance parts of the program, so I’m not sure if making a class instead of a struct would be the best idea. What would it be the best solution here, if performance is the main concern (more than clearness actually)?
For a program I’m making I’m using object to hold the value of HTML
Share
You can create a type alias like
using AttrValue = System.Object;. However, this alias will only exist in your source code. There’s nothing stopping you from using, say,objectorstringwhere you need anAttrValue. You won’t be able to add properties or methods onto yourAttrValuealias: it will be an alias forobjectand not a class in its own right.You’re probably better off introducing
AttrValueas a class in its own right, presumably as a wrapper for a value of typeobject. It might have a single field (of typeobject), and a constructor that takes oneobjectparameter.Regarding struct vs. class, I wouldn’t worry about this. You almost never need a struct in .NET code: the garbage collector is capable of handling lots of small class instances without noticeable overhead, and structs have their own oddities (mainly because what looks like the same instance in source code can easily end up being two separate copies at run time).