You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I got tired of redefining the same HTTP constants in every single project I work on, because sometimes I just need a compile time constant (switch ... case ..., attributes, default method argument value). And I want to ask: why for the love of God constants were completely removed?
I've read the discussion on #9514 when a change was made to improve string comparison performance for HeaderNames and I've seen multiple people suggesting the obvious solution of using backing const field for static readonly fields.
Like what was preventing a similar implementation?
publicstaticclassHttpMethods{// We are intentionally using 'static readonly' here instead of 'const'.// 'const' values would be embedded into each assembly that used them// and each consuming assembly would have a different 'string' instance.// Using .'static readonly' means that all consumers get these exact same// 'string' instance, which means the 'ReferenceEquals' checks below work// and allow us to optimize comparisons when these constants are used.// Please do NOT change these to 'const'/// <summary>/// HTTP "CONNECT" method./// </summary>publicstaticreadonlystringConnect="CONNECT";/// <summary>/// HTTP "DELETE" method./// </summary>publicstaticreadonlystringDelete="DELETE";/// <summary>/// HTTP "GET" method./// </summary>publicstaticreadonlystringGet="GET";/// <summary>/// HTTP "HEAD" method./// </summary>publicstaticreadonlystringHead="HEAD";/// <summary>/// HTTP "OPTIONS" method./// </summary>publicstaticreadonlystringOptions="OPTIONS";/// <summary>/// HTTP "PATCH" method./// </summary>publicstaticreadonlystringPatch="PATCH";/// <summary>/// HTTP "POST" method./// </summary>publicstaticreadonlystringPost="POST";/// <summary>/// HTTP "PUT" method./// </summary>publicstaticreadonlystringPut="PUT";/// <summary>/// HTTP "TRACE" method./// </summary>publicstaticreadonlystringTrace="TRACE";}
After reading the discussion this seems so obvious and it seems to get best of both worlds: ability to use ReferenceEquals optimization with an option to have a compile time constant when needed. And the solution isn't event complicated.
Can anybody elaborate and explain to me what am I missing here? What are your thoughts on this?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I got tired of redefining the same HTTP constants in every single project I work on, because sometimes I just need a compile time constant (
switch ... case ...
, attributes, default method argument value). And I want to ask: why for the love of God constants were completely removed?I've read the discussion on #9514 when a change was made to improve string comparison performance for
HeaderNames
and I've seen multiple people suggesting the obvious solution of using backingconst
field forstatic readonly
fields.Like what was preventing a similar implementation?
Why was this the preferred solution?
After reading the discussion this seems so obvious and it seems to get best of both worlds: ability to use
ReferenceEquals
optimization with an option to have a compile time constant when needed. And the solution isn't event complicated.Can anybody elaborate and explain to me what am I missing here? What are your thoughts on this?
Beta Was this translation helpful? Give feedback.
All reactions