public static T AllocateMemory<T>(Func<T> func)
catch (System.OutOfMemoryException e)
Console.WriteLine("Generic memory error " + e.ToString());
private readonly string _arg1;
private readonly string _arg2;
public MyClass(string arg1, string arg2)
public override string ToString()
return string.Format("{0} {1}", _arg1, _arg2);
class MyClassWithPrivateCTor
private MyClassWithPrivateCTor()
static public MyClassWithPrivateCTor Create()
return new MyClassWithPrivateCTor();
public static void Main()
var o = Library.AllocateMemory(() => new MyClass("Foo", "Bar"));
var p = Library.AllocateMemory(() => MyClassWithPrivateCTor.Create());