Reputation: 2547
There was a previous question related to an implementation point in .Net standard's System.Net
, regarding why the HttpMethod
was not still an enum.
Why is System.Net.Http.HttpMethod a class, not an enum?
It has a great answer around extensiblity.
However, as I say in the title why is it not a struct?
It seems to pass all the tests according to microsofts guidelines? It's a bit of info that gets thrown around an awful lot in web applications and I 'think' DELETE
is the longest verb and comes in below the 16 bytes recommended.
Is this something to do with strings being a halfway house in .Net? I know gains might be marginal, but I just want to understand better.
Upvotes: 2
Views: 1542
Reputation: 2605
There would not be a lot of advantages in struct over class.
Structs are perfect for memcpy / memcmp types of implementations and in case of HttpMethod
it's not necessary.
if you look at .net core implementation https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/HttpMethod.cs you will see that it uses reference equality check to compare instances which is slightly faster than string comparison. With structs it would not be possible... Since most struct instances would actually be different instances and they would have to compare string values.
Given that HTTP method string values aren't extremely long there are really no other benefits in struct here, as it only carries one string reference and most HttpMethod instances are going to be instances of static fields of HttpMethod class.
It could also be a plain string but then it would not aid in method resolutions during compilation and such.
Upvotes: 1