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
Dultus2y 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
Dultus2y 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
Dultus2y 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
Dultus2y 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
More Posts
❔ Newtonsoft, count nested propertiesSome time ago I had a nice chat in here where we found a solution to a problem. Namely counting all list that holds 2 values eachI want to have a way to store a dynamic amount of data, where each entry holds a string and an int. ❔ How to close a process's handle?Hey folks, I'm trying to automate something that I've been doing manually through Process Explorer -❔ ObjectListView showing blank rowsHi all. I'm new to C# and making VSTO addins, please bare with me! I'm using the add-in control O❔ NullReferenceException Object reference not set to an instance of an object Billboard.UpdateHello, my name is Zorphix and I'm not so smart at C#. And I'm currently trying to add a billboard sc❔ Blazor Hosted - Client datatable with large datasetI have a table component (using MudBlazor) which accesses the server-side API endpoint and pulls (cu❔ How do I cycle through different observable collections within the same collection view?My current plan is to do something like: ``` List <ObservableCollection<TodoItem>> allTodoItems✅ I'm trying to understand WPF's source code. What is a WeakReferenceWeakReference is a class that probably has applications other than in WPF (in particular, I'm trying❔ Shortcut for selecting multiple lines on windowsHi, I want to select only lines 146, 150 and 154 in my project in order to copy them, is there a sho✅ Token present on Get but missing on Post using IdentityPasted images to show autos with breakpoints. Code present on Get but missing on Post. I'm not sure