C#
C#

help

Root Question Message

Dultus
Dultus1/3/2023
❔ Is there any overview of useful attributes and how to use them?

E.g. [Obsolete] or for example where a method is allowed to be used and so on.
Basically basic attributes for daily things that are included in common libs like System.
AntonC
AntonC1/3/2023
MethodImpl, DllImport, Flags, AttributeUsage
assembly info attributes
nullability hint attributes
the debugger attributes
the new .NET 7 attributes for parameters
attributes for source generation, like the .NET 7 regex
serialization attributes
AntonC
AntonC1/3/2023
google each one for info
AntonC
AntonC1/3/2023
there's a whole slew of others if you're working under a framework like aspnet core
FusedQyou
FusedQyou1/3/2023
All attributes are useful
FusedQyou
FusedQyou1/3/2023
It just depends on what you need
FusedQyou
FusedQyou1/3/2023
Like this is specific as hell and really depends on if you even need it
AntonC
AntonC1/3/2023
I would guess the author was tasked to make a presentation
FusedQyou
FusedQyou1/3/2023
Most people won't even use MethodImpl or DllImport for example
AntonC
AntonC1/3/2023
they need examples of most common ones
FusedQyou
FusedQyou1/3/2023
And you chose some of the least common ones 😛
FusedQyou
FusedQyou1/3/2023
I wonder what anybody would even gain from this
AntonC
AntonC1/3/2023
these seem pretty ubiquitous to me
FusedQyou
FusedQyou1/3/2023
AttributeUsage yes, Flags maybe
FusedQyou
FusedQyou1/3/2023
The rest not really
FusedQyou
FusedQyou1/3/2023
Attributes are way too broad in their use case. You can't call one more useful than the other
FusedQyou
FusedQyou1/3/2023
[Obsolete] is more often used if you develop a library and decide to make a breaking change that would effect end users. It is a way to inform them of this and in pretty much all cases it contains information on what must be done in order to migrate
FusedQyou
FusedQyou1/3/2023
You won't really use this in personal projects
Dultus
Dultus1/3/2023
Nah, I just want to get more into them.
Dultus
Dultus1/3/2023
I'm aware but that was an example. 😄
Dultus
Dultus1/3/2023
I want to write more clear and cleaner code - even though yes, there's patterns that do that I really appreciate descriptive methods.
FusedQyou
FusedQyou1/3/2023
If your aim is cleaner code, attributes won't really help you with that
FusedQyou
FusedQyou1/3/2023
Applying better practices such as guard clauses or proper naming conventions can really help with that, but there's a lot to it
Dultus
Dultus1/3/2023
I know! I've been C++ programmer.
I just want to learn 'more'. Clean also means descriptive.
FusedQyou
FusedQyou1/3/2023
MethodImpl is something that could improve the performance of your code but it is not guaranteed. It's better to just let the compiler handle any extra performance improvements
FusedQyou
FusedQyou1/3/2023
DllImport might work for you in order to call native c++ code if you want.
FusedQyou
FusedQyou1/3/2023
Other than that, Flags and AttributeUsage are way too specific, and in general it really just depends on what you work on, and won't make your code "better"
Dultus
Dultus1/3/2023
Better to use though. 😄
FusedQyou
FusedQyou1/3/2023
You mean Flags or AttributeUsage? Flags is only used with an enum if you use it as a bitfield, which can be often or never at all. AttributeUsage is completely optional and might be used to restrict your attribute usage, which might not be required at all.
FusedQyou
FusedQyou1/3/2023
So, usecase 🙃
Dultus
Dultus1/3/2023
Yeah, sure! Not saying anything against that. 😄
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy