-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[API Proposal]: Need a Reliable Way to Check to See if a Type is a Record Class #108531
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection |
The concept of record doesn't exist at runtime. It's just a convention used by C# compiler.
It's mostly incorrect. The performance characteristics isn't determined by it category of declaration, but the way they implemented the conventions. For example, the concept of "clone" doesn't exist for arbitrary class type. An clumsy implementation with reflection will be obviously slow. Value types have fallback implementation of equality and hash code, which are far from ideal. Record types are just usual classes or structs (with Moreover, it's intentional to make records not distinguishable with regular types. See discussions in dotnet/csharplang#39 (comment) dotnet/csharplang#39 (comment) and related issues. |
You haven't explained what actual purpose you'd use this method for - what concrete difference it would make in your code. The only thing that would be available "globally" would be |
This issue has been marked |
Closing; C# record types are either standard classes or value types with generated members so there is not an accurate way to check if the type was based on a record. For the original case on perf, individual checks for constructor overloads and member overrides would be a better approach as it would also work with non-record types as well. |
Background and motivation
In .NET, the Type class provides various methods to determine whether a type is a value or a class type. Through my benchmarking work with .NET, I've discovered that the performance characteristics of these types can vary significantly. Additionally, as discussed in detail in my article (link below), performance differences also exist between record types, specifically
record
classes.Given these variations, it's important to have a reliable way to identify if a type is a record class. This distinction is critical for writing efficient generic methods that can handle value types, classes, and record types differently.
Currently, I am using the following code to check if a class is a record type, but I'm looking for a more robust and reliable approach.
https://dotnettips.wordpress.com/2021/02/26/everything-you-want-to-know-about-the-record-type-in-net-5-but-were-afraid-to-ask/
API Proposal
API Usage
Alternative Designs
No response
Risks
None that I can think of since this will be a new method added to the Type type.
The text was updated successfully, but these errors were encountered: