CLR Generics and Struct Boxing

The PR comments from @benaadams here are awesome. https://github.com/dotnet/corefxlab/pull/2358
This is likely basic knowledge needed for high-performance .NET, but it was new to me. Very interesting to see how defining a generic <T> can eliminate boxing/allocations.

The T parameter is explicitly being specified as an interface: IBufferWriter<byte>; so if you create that BufferWriter<IBufferWriter<byte>> and give it a struct as the T, then its boxed to the interface and stored in the BufferWriter<IBufferWriter<byte>> as the boxed IBufferWriter<byte> type.

So, if you explicitly specify the T, it will get boxed. If you specify it as a generic, and then call the generic constructor, it will not.

public static JsonWriter<TBufferWriter> Create<TBufferWriter>(
TBufferWriter bufferWriter,
bool isUtf8,
bool prettyPrint = false)
where TBufferWriter : IBufferWriter<byte>
{
     return new JsonWriter<TBufferWriter>(bufferWriter, isUtf8, prettyPrint);
}
Michael

I'm a software developer and consultant living in Cary, North Carolina. I work for a small consulting firm based out of Clemmons, North Carolina. We have clients all over the state. I've had amazing opportunities to work with different technologies and business problems since getting started in 2009. My goals for this site are simply to share interesting things I run into or do.