This is an academic question. Which one is better? or Even though both Foo and Bar do the same thing, Foo is better performance wise. The compiler converts Foo into IL which roughly achieves the same thing as the following pseudo-code. Foo when converted to IL splits the search space with 110 in the middle… [Read more…]
In an internal meeting of a bunch of developers at my current company, someone made the assertion that the Switch statement in C# (or Select Case in VB.NET) is just a compiler short-cut for writing a chain of If-Then-Else statements. What do you think? Is that statement – true or false? Take the following C#… [Read more…]
Problem: Consider the following generic function written in VB.NET: What message do you expect to be shown by the following bit of code? Would the “Foo” method even compile? It doesn’t make sense to assign a Nothing (VB.NET equivalent of C#’s null) to a value-type, right? Solution: Well, the code compiles just fine. When synthesizing… [Read more…]
Problem: Do warning bells go off in your head when you see code like the following? Are there any problems you need to be aware of when declaring events inside structures? Solution: Well, code like the following wouldn’t work, even though it seems to be perfectly reasonable. foo_SomeEvent will actually never be called. Why? Remember… [Read more…]
Ricardo Parker, over at SeattlePI, while talking about Tata Nano said, “I’m sure Indians would much rather run on electricity than gasoline if the cost of the cars was the same”. While I’m sure his post is full of material to attract wonderful comments from Indian readers in any case, I thought I’ll add something… [Read more…]
Problem: I had used .NET Mass Downloader tool to download .NET 2.0 Framework sourcecode some time ago. While reading Timer.cs (System.Windows.Forms.Timer) and a couple of other classes I noticed something interesting. Does the following piece of code look odd to you? Notice the empty “try” block and all processing being done in the “finally” block.… [Read more…]
Problem: .NET 2.0 introduced the GZipStream class to allow programmatic compression and decompression of files. However that class doesn’t provide an easy way for us to determine if a file is actually a compressed file. Decompressing the entire file just to determine if a file is actually compressed is wasteful. Solution: Most files have a… [Read more…]
You might get an exception that says “The process cannot access the file C:\bla\bla.bin because it is being used by another process” when trying to read all bytes at once using File.ReadAllBytes(). While it is arguable if you should be reading an entire file into memory at once, but in here we’ll assume you do… [Read more…]
If you’re reading this article, you’re most probably familiar with the InvokeRequired property on Windows.Forms.Control class which we have to use whenever setting a property on a Windows Forms control from a thread other than the one it was created on. Here is what the MSDN page about InvokeRequired says: Controls in Windows Forms are… [Read more…]
Look at the following piece of code: Note how we are first assigning “Hello”, then appending ” “, and then appending “World”. The C# compiler generates the following IL code for our method named Test. From the IL you can see that the compiler did some optimization. The compiler saw what we were actually… [Read more…]
April 24, 2008
1