C# has low barriers to entry and forgives a lot. Seriously, you may not understand how things work under the hood but still write code and remain easy-going about this. Though you still have to deal with different nuances over time. Today, we'll look at one of such subtle aspects — handling enumerations.  Rarely do we get the chance to find a developer who hasn't encountered enumerations. However, anyone can make an error when using them. It is more likely if: Besides, in practice the problems below may not be issues for your application. However, if such code repeatedly executes (like tens of millions of times) and starts inconveniencing, you'll already know what you're dealing with. Note. All the research we will be doing below has been done for .NET Framework. It's an important comment. We'll talk about .NET a bit later.  I encountered this problem not long ago when I was dealing with various optimizations of the C# PVS-Studio analyzer. Yes, we already had one article on this subject, but I think there will be more.  During this process, I was fixing various places in code. As practice has shown, even small edits can boost performance if made in the app's bottlenecks. At some point, based on the profiling results, I got to VariableAnnotation class. We'll consider its simple version: Now let's write two simple methods in which: Corresponding methods: If you run any of these methods and watch the application in dynamics, you can note nasty specifics: it puts pressure on GC. For example, this can be seen in Visual Studio ''Diagnostic Tools'' window.  Process Hacker on the ".NET performance" tab of process information also shows this.  Above examples clearly indicate two culprits: Let's deal with them one-by-one. Here's the code we'll investigate next: The first thing experts will pay attention to is that there is no Enum.Equals. IDE will help here, by the way. In this case, the Object.Equals(object objA, object objB) method is called.  The IDE itself drops a hint about this:  We work with instances of value type, whereas we need reference types to call the method. Therefore, boxing will take place before the method call. By the way, if you look into the IL code, you can find boxing commands: Here we clearly see the call of the System.Object::Equals(object, object) method. The command of arguments boxing — box (IL_0001, IL_0007) — is also called. Since we box objects only to call the method, the corresponding references are not saved anywhere. Hence, the boxed objects will be cleaned up during garbage collection. Note. Someone may say — everyone can see that Enum.Equals == Object.Equals. Look, even IDE highlights this. The answer is no, no, and again no. The simplest proof is that such code was written. And I'm sure some developers use a similar way of comparison. As for "obviousness", very often people fall into the trap of thinking that if something is obvious to them, it's obvious to everyone. That's not the case. If we change the Enum.Equals call (in fact — Object.Equals) to compare through '==', we get rid of unnecessary boxing: However, we should remember that the generic code version (the VariableAnnotation type was generic) will not compile: Calls of instance Enum.Equals and Enum.CompareTo methods will not work out for us—they entail boxing. The way out can be the generic EqualityComparer<T> type. For example, one can safely use a default comparator. The code will roughly look as follows: The EqualityComparer<T>.Equals(T x, T y) method receives arguments of generic type, and therefore does not require boxing (at least before its call). Inside the method call, it's okay too. Boxing commands are gone in the IL command code: Visual Studio profiler doesn't capture any garbage collection events in this code.  Process Hacker indicates the same thing.  You might become interested in how EqualityComparer<T> really works on the inside. As for me, I got curious. The source code of this type is available, for example, at referencesource.microsoft.com. Now consider what is going on with the Enum.GetHashCode method. Let's start with the following code: You may be surprised by what is happening here: boxing and as a result the GC pressure. The profiler and Process Hacker signals us about this again. So why not indulge yourself and get nostalgic? Let's compile this code via Visual Studio 2010. We'll get the IL code like this: Everything seems to be expected: the box command is in the right place (IL_0001). This answers the question where the boxing and the GC pressure come from. Let's return to the modern world and now compile the code in Visual Studio 2019. We got the following IL code: Suddenly, the box command disappeared (just like a pencil in "The Dark Knight"). Yet the boxing and the GC pressure remained. At this point I decided to check out the Enum.GetHashCode() implementation at referencesource.microsoft.com. The most intriguing part here is the comment "Avoid boxing… ". It's like something doesn't add up... Boxing must be missing, as well as the box command in the IL code. But memory allocation in the managed heap and garbage collection events are in place. Let's see the CIL specification to get a better deal with IL code. I cite the method call again so that you have it right in front of your eyes: As for the ldarga.s instruction, it's all simple. The address of the method argument is loaded to the evaluation stack.  Next comes the constrained. prefix. Prefix format:  Stack transition: Depending on what thisType is, the way the ptr managed pointer is handled differs: As noted in the specification, the latter case is only possible when the method is declared in System.Object, System.ValueType, and System.Enum and not is overridden in the child type. The second case in the list above allows you to exclude an object boxing when a method is called, if possible. But we faced a third case. GetHashCode is overridden in System.Enum. System.Enum is the base type for OriginType. However, the enumeration itself does not override the methods from System.Enum. This is why the boxing happens when they are called. I'd like to emphasize that this is relevant for any value types. If you don't override the base method, the object will be boxed to call it. But let's go back to the enumerations. We can't override the method in enumeration. So what can we do with them? The System.Collections.Generic.EqualityComparer<T> type that I have mentioned before may be really helpful here. This type contains the generic GetHashCode method — public abstract int GetHashCode(T obj): As I said earlier, everything said above was relevant to the .NET Framework. Let's see how things are going in .NET, shall we? As expected, boxing is present. No surprise here, as we still need to call the Object.Equals(object, object) method. So it's not worth comparing enumeration elements in this way anyway. Speaking about the Enum.Equals instance method, the argument still has to be boxed.  And this is where a nice surprise was waiting for me! Let's recall the code example: Let me remind you that when you run this code in .NET Framework, new temporary objects are created because of boxing. The result is additional GC pressure.  But nothing similar happens when using .NET (and .NET Core)! No temporary objects, no GC pressure.  Okay, we kind of dealt with the boxing issue. Let's move on to the performance question. At the same time, we'll compare the speed of the same code for .NET Framework and .NET. All the code for the compared methods is the same. There will be two differences: how we compare enumeration elements and how we get hash codes. Description of comparison ways used in methods: Execution times are compared below. .NET Framework 4.8  .NET 5  I'm thrilled with the results of EqualityComparer<T> on .NET 5. As for the performance, we got about the same time as in direct comparison of enumeration items. Kudos to Microsoft! When you update the target framework/runtime, you get optimization out of the box without changing C# code. Description of ways to get hash code used in methods: The first and the last points are clear now. The second and third are hash code hacks, inspired by Enum.GetHashCode and Int32.GetHashCode implementations. They are still unresistant to changes of underlying type and not very obvious. I'm not encouraging to write like this. Yet I added them to the tests for the sake of interest. Execution times are compared below. .NET Framework 4.8  .NET 5  We've got 2 good news at once: C# is cool. You can code in it for years and not know about nuances related to basic things: why out -parameters can remain uninitialized; why the result of nullable-value boxing can be null; why boxing happens when you call GetHashCode for enumerations. And when you have to deal with something like this, it may be extremely engaging to get the point. I'm getting high from that. I hope you do as well. As usual, consider subscribing to my Twitter so you don't miss out on anything noteworthy. Независимый эксперт