-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Need an efficient helper to check if an instance of T is a default(T) #20240
Comments
CC @jkotas |
Yes, definitely, this would be awesome. A cost-free API for checking whether some generic type is a class (and not a nullable) would be nice too, if it can be implemented straightforwardly. |
Do you have more examples where this would be needed? It is not a rocket science to write such helper - take address of the value and its size, and check whether it is all zeros (using unsafe code, potentially with the usual loop unrolling, etc.).
Agree. It should be separate issue. |
If it could work on a boxed value, I'd use it in |
I've implemented prototype of such method as a part of my library so the code can be reused. Source code is here and the public method is here.
Additionally, I have However, I used inline IL for implementation to keep the full control over generated code. But I'm sure that the same behavior can be achieved using |
The need arises once in a while to check if a value at hand is not initialized.
See for example comments on dotnet/corefx#16122 (comment)
Also, If/when parameterless constructors for structs are introduced, the need for
IsDefault
check could become more apparent.The text was updated successfully, but these errors were encountered: