C#C
C#3y ago
AndyB

❔ 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.
private static byte[] StructToBytes<T>(T data) where T : struct
{
    byte[] rawData = new byte[Marshal.SizeOf(data)];
    GCHandle handle = GCHandle.Alloc(rawData, GCHandleType.Pinned);
    try
    {
        IntPtr rawDataPtr = handle.AddrOfPinnedObject();
        Marshal.StructureToPtr(data, rawDataPtr, false);
    }
    finally
    {
        handle.Free();
    }

    return rawData;
}
Was this page helpful?