Hello, I just started learning C# attributes, some times it be like this [xyz] and sometimes [xyz(a)] and sometimes like [xyz = bla bla bla]
What is it? Why am I seeing it everywhere but yet there is no much explanation about it in the internet?
like is it some kind of constructions to the compiler? or is it some utility helpers? or does it automate something in the code, I am just completely lost
Rajanikant HawaldarPosted Jun 28, 2022, 6:43 PM
It's like meta data associated with class or members (not instances)
for the first question, attributes are defined in classes and what you need to pass depends on their constructor and what parameters it takes
[xyz] is a parameterless one, if the constructor has mandatory parameters (i.e. parameters with no default) you'll have to pass them in the parenthesis, you need = for named parameters
https://www.c-sharpcorner.com/article/walkthrough-c-sharp-attributes/
https://www.c-sharpcorner.com/UploadFile/84c85b/using-attributes-with-C-Sharp-net/