❔ 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.
16 Replies
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
google each one for info
there's a whole slew of others if you're working under a framework like aspnet coreAll attributes are useful
It just depends on what you need
Like this is specific as hell and really depends on if you even need it
I would guess the author was tasked to make a presentation
Most people won't even use
MethodImpl
or DllImport
for examplethey need examples of most common ones
And you chose some of the least common ones 😛
I wonder what anybody would even gain from this
these seem pretty ubiquitous to me
AttributeUsage
yes, Flags
maybe
The rest not really
Attributes are way too broad in their use case. You can't call one more useful than the other
[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
You won't really use this in personal projectsNah, I just want to get more into them.
I'm aware but that was an example. 😄
I want to write more clear and cleaner code - even though yes, there's patterns that do that I really appreciate descriptive methods.
If your aim is cleaner code, attributes won't really help you with that
Applying better practices such as guard clauses or proper naming conventions can really help with that, but there's a lot to it
I know! I've been C++ programmer.
I just want to learn 'more'. Clean also means descriptive.
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
DllImport
might work for you in order to call native c++ code if you want.
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"Better to use though. 😄
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.
So, usecase 🙃Yeah, sure! Not saying anything against that. 😄
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.