Yet Another Way To Create An Object

After I wrote this post, I come up with yet another way to create an object... Here it is:

Stopwatch watch = new Stopwatch();
ConstructorInfo ci = typeof(StringBuilder).GetConstructor(new Type[0]);
NewExpression expr = Expression.New(ci);
Func<StringBuilder> func = Expression.Lambda(typeof(Func<StringBuilder>), expr).Compile() as Func<StringBuilder>;

watch.Start();
			
for (Int32 i = 0; i < 100; ++i)
{
    StringBuilder builder = func();
}

Int64 time4 = watch.ElapsedTicks;

watch.Reset();

Bookmark and Share

                             

No Comments

Add a Comment

As it will appear on the website

Not displayed

Your website