-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Make Getting sizeof(T) where T : struct Possible #3208
Comments
Updated based on the conversation happening in #3210 |
@whoisj while I would obviously agree with you that this is very handy and should be supported directly in c#, isn't something along the lines of http://stackoverflow.com/questions/18167216/size-of-generic-structure a temporary workaround for you? (With very little performance overhead...) |
The solution presented on SO seems reasonable, but I'm not certain about the runtime costs associated with passing value through the marshaler to get its size. Still, it seems to me to be a work-around for a missing feature, especially when the underlying IL supports the operation and it is C# which imposes the limitation.
|
There is also DotNetCross.Memory.Unsafe that exposes some IL instructions as regular .NET methods that are unavailable in C#. The library is implemented using IL instead of C#. For instance: var size = Unsafe.SizeOf<T>(); In my opinion, however, most of the stuff exposed by the library should be available directly in C#. |
|
Moving language design discussion to csharplang. Thanks |
Issue moved to dotnet/csharplang #1032 via ZenHub |
I took often find myself writing long lists of methods (like below) where I'm reading the bits of stack values for things like hashing, serialization, encryption, etc. because I'm unable to acquire
sizeof(T)
evenwhen T : struct
which I I find fairly frustrating.Ideally the
sizeof(...)
operator would work on generics so long as the type being measured is guaranteed to be a value type (ala struct).This should be perfectly legal code:
Then I would only need a single method and not a dozen.
Support for
T*
is #3210The text was updated successfully, but these errors were encountered: