❔ Is `where` still used?
I'm looking at creating some functions to convert byte[] to Structures and back, and all examples I see are very old. They use the keyword "where" which I've never seen before. Is this still current or has it been depricated?
e.g.
26 Replies
the
where
keyword (it's not deprecated) constraints T
, in this case, to be a struct.
for example I could also constraint T
to be part of a class named Node
for that, i'd just type where T : Node
so if you try to pass in something that is not a struct, it'll give you an errorCopy, TY. So are you saying the code would still work without the
where
but using it will help in debugging?in some cases it would work, in some it wouldn't
but no, it wouldnt work
Ok. For some reason, I hadn't seen that keyword before.
because if you remove the struct constraint, if the code expects a struct anywhere within the functions
it wont be sure that T is a struct, and therefore will give you an error
Would you say this is still a valid way to copy bytes into a fixed structure, or are there more modern (.NET 8) ways to do this?
Assuming the
struct
is formed correctly with LayoutKind.Sequential
and so on...you could use a span to avoid the heap allocation of the byte array
Go on...
https://stackoverflow.com/questions/50285147/how-get-a-spanbyte-view-of-a-struct-without-the-unsafe-keyword take a look here
Stack Overflow
How get a Span view of a struct without the unsafe keyword
How can a Span<byte> view (reinterpret cast) be created from a single struct value with no copying, no allocations, and without the unsafe keyword.
I can currently only accomplish this usi...
I can currently only accomplish this usi...
keep in mind that a span is a reference to existing memory
TY. It says "partial solution", is this valid code?
Hadn't heard of
MemoryMarshal
before.for what i understood this works if you're not targeting netstandard2.0
I'm using .NET8
you should be fine
Looks simpler, and no try/catch needed?
nope
it's just a cast
Very cool. Just have to use
Span<byte>
instead of byte[]
when reading the data. TY!though i don't know how safe is this code because i don't know how it is used
so make good judgment whether to use
byte[]
or Span<byte>
When working with hardware or embedded devices, It's a very common part of C/C++ software where you read byte data from a device and then manipulate it with a structure. C# being managed doesn't seem to have this built-in ability, so just looking to create some generic functions. Appreciate the help!
there are many options to use C# in an "unmanaged way", you should take a look to the
unsafe
keyword, the classes MemoryMarshal
, Unsafe
etc...
so just be careful that span is like a pointer, if the memory it points to is invalid, it will trigger an undefined behaviorIt's definitely not deprecated in any way. Type parameter constraints (
where T :
) are used all around for various purposes, everything from in your example constraining types arguments to only value types, to constraining to types implementing certain interfaces. The numeric interfaces introduced in .NET 7 hinges on constraints to be useful in the first place. Constraints are alive and well.
eg. And you can safely pass references of T where the constraint type is expected without casting.
Thanks everyone. TIL about “where” !
What about this?
??
what about this.. what?
This is some code containing several words and special characters.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.