C
C#2y ago
Dultus

❔ 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
Anton
Anton2y ago
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 core
FusedQyou
FusedQyou2y ago
All 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
Anton
Anton2y ago
I would guess the author was tasked to make a presentation
FusedQyou
FusedQyou2y ago
Most people won't even use MethodImpl or DllImport for example
Anton
Anton2y ago
they need examples of most common ones
FusedQyou
FusedQyou2y ago
And you chose some of the least common ones 😛 I wonder what anybody would even gain from this
Anton
Anton2y ago
these seem pretty ubiquitous to me
FusedQyou
FusedQyou2y ago
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 projects
Dultus
DultusOP2y ago
Nah, 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.
FusedQyou
FusedQyou2y ago
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
Dultus
DultusOP2y ago
I know! I've been C++ programmer. I just want to learn 'more'. Clean also means descriptive.
FusedQyou
FusedQyou2y ago
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"
Dultus
DultusOP2y ago
Better to use though. 😄
FusedQyou
FusedQyou2y ago
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 🙃
Dultus
DultusOP2y ago
Yeah, sure! Not saying anything against that. 😄
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server