I got this exception while trying to create a new instance of the X509Certificate2—to have a public certificate in-memory. Obviously, the error message was not that helpful.
It turns out that the content of the byte I passed into the constructor was not correct. It was only the actual public-key (cert.PublicKey.RawData), and the rest of the public-key-cert (cert.RawData) was missing (e.g. name, expiration date).
I have no doubt other conditions could cause this exception, but this could be what you're looking for :-)
The implementation of String.Join internally calculates the expected output length. If the calculated length is greater than max int32 (~2 billion characters), it will throw an "OutOfMemoryException" even though the system is not out of memory at that time .
It takes about 4GB to store 2 billion characters; since .NET stores strings in UTF-16 format, two bytes per character. However, .NET has an array memory limit of 2GB (unless on a 64-bit environment with <gcAllowVeryLargeObjects> enabled). As the code comment noted, this pre-check only catches the extreme case. The remaining of the method may still cause an OutOfMemory exception even if the pre-check passes.
For the record, .NET has an InsufficientMemoryException class for situations like this. However, this exception type was introduces in .NET 2.0. To stay backward compatible, the method remains throwing the OutOfMemoryException.
The following snippet comes from the official implementation of the method:
Recently, someone from our team sent out a code review. I noticed that the call to base() from some class contructors were automatically removed by ReSharper. At first I didn't think it would make a difference, then I got curious and did a little testing. There's a subtle difference between explicitly calling the base() constructor or let the base constructor called on by .NET.
The code below results in the following output:
With or without the base() call, the base constructor is called anyway. It appears that specifying base() calls the parent constructor as if the method is being called from the child constructor. This behavior seems to be consistent among .NET 4.5, 4.5.1, 4.5.2, 4.6, and 4.6.1. I have not tested this on a machine without .NET 4.6.