Hacker News new | ask | show | jobs
by wesnerm2 1416 days ago
C# had value types and pointers from the very beginning. These are not a recent addition. The standard library does know about them. However, not until C# 2.0, which introduced generics, were collections able to avoid boxing value types.

There are some cases where allocations are made when they could have been avoided. Iterating over a dictionary creates a single IEnumerator object. Async methods, tuples, delegates, and lambda expressions also allocate memory as do literal strings. It is possible to have struct-based iterators and disposers. There are some recently added mitigations such as a ValueTask, ValueTuple, function pointers, ref structs, conversions of literals to read-only spans, that eliminate allocations.

DateTime is a value type and doesn't allocate memory. Getting the current time does not allocate memory.

With the recent additions to ref types and Span<>, C# provides a lot of type-safe ways to avoid garbage collections. You can always use pointers if need be.

1 comments

And to put that in context, 2.0 was around 2003, iirc
It was 2005.

But even then, arrays of value types were available since C# 1.0 (2001).