Some while ago, i saw the following code in a project i’m working:
string[] a = (string[])Array.CreateInstance(typeof(string), 10);
And then i became shocked… I called him every name in the book. And why? Because the complication above could be replaced simply by:
string[] a = new string[10];
And there are 2 reasons:
First: The IL generated by the CreateInstance is much more complex than the IL generated by the new operator:
Create Instance IL:
IL_0000: ldtoken [mscorlib]System.String
IL_0005: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_000a: ldc.i4.s 10
IL_000c: call class [mscorlib]System.Array [mscorlib]System.Array::CreateInstance(class [mscorlib]System.Type,
int32)
new operator IL:
IL_0011: castclass string[]
IL_0016: stloc.0
IL_0017: ldc.i4.s 10
IL_0019: newarr [mscorlib]System.String
Second: because the CreateInstance uses reflection and, because of that, the operation is much more slow. Here are the times, in Miliseconds, for 1 million iterations of each operation:
CreateInstance: 406,2422 ms
New: 31,2494 ms
Has you can see, reflection is very expensive. In this case, just the line of code with CreateInstance is 13 times more expensive then the one with the new operator.
So, be careful where and when you use reflection. The bad use of it can result in a performance break of your application.
I would add a third reason:
third: why complicate your life and code with reflection when you can just use pure C# code?
This also applies to Java of course, which is my main language
Riccardo, I agree. In fact I wouldn’t say its the third reason. The first and only important reason is because when you read the code it is obvious what it does.
Because the compiler has generated more code, or is slower, is largely irrelevant. That is an issue for the compiler writers to ponder.
Riccardo you are 100% correct. I actually forgot the most obvious reason of all
Ok
I also agree with Matt: new string[10] talks for itself, array.createInstance… not that much!
The thing I don’t understand about this, is why?
It’s a bit like what Riccardo said, as to why someone would complicate their life this much over something so trivial. I’m not sure whether the original author (of the uselessly “reflected” code) was a genius or just plain stupid?
Happy Valentine’s Day!!!
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Did you hear about the dyslexic lawyer? He studied all year for the bra exam
Okay extremely I have had The car as with regards to three weeks Today. this wasn’t until a week later when I was operating The vehicle at evening, I noticed the The headlights on The auto and also including The interior lights were dimming slightly and also Getting brighter again (every two seconds The lights may dim, but After that go rear how to normal) by the very same time, I also heard a faint whining noise which seemed To Follow The very same pattern a warm or hot environment the lights (off and in, not continuous). the following morning I drove the vehicle, and also even via The headlights off, the whining continued. I also checked if it i has subsequent out of the AC, rather which i has never this. it didn’t routinely come about every time turned The car by. this seemed liking it would happen with regards to ten minutes after I started driving it. as well, the headlights only flickered when the engine i has at. a lot I decided To get this how to The dealership. They said this i has The alternator, ever so they replaced this as $700. Four days passed after they anchored it and also The auto was OK. it wasn’t until yesterday I noticed The very same thing most during Once more. may someone say to me What is improper? (The automobile is into excellent disaability otherwise with only 50,000 miles going on this).
That is most likely the most effective article that ever cross my reference. I don’t see why anyone should disagree. It could be too simple #for them# to comprehend…anyway nice work i am coming back right here for More Great Stuff!!
Sitemizde sa? ekimi,burun esteti?i,meme k???ltme,meme esteti?i,sa? ektirme,sa? ekimi merkezi,meme b?y?ltme,fue,sa? ekimi fiyat?,sa? ekimi merkezi,sa? mezoterapisi,sa? ekimi sonras?,sa? nakli,ka? ekimi,ka? ektirme,fue sa? ekimi,estetik g?z hakk?nda bilgiler bulunmaktad?r. http://www.aytugluhairtrans.com
I really like this particular weblog, thanks a bunch for the information
Thanks, looks superb tech. geeks win.
Get free web hosting
Hello, Hi, Hey, great article, post, blog, I, we love, like, loved, liked it !!!
HI,I like your post. its really helpfull
A fresh iPhone unlock software from Best Unlock iPhone, has been introduced to enable users to jailbreak and unlock iPhone 4/3Gs/3G Any iOS.Guide, tutorial and software is guaranteed to unlock and jailbreak your iPhone 4S/4G/3Gs and 3G easy and safe.
This was a really an interesting post. I learned several of new things by reading it. Thank you so much for writing this article it was a true pleasure to read it.