Switch vs if-else-if

April 24, 2008

1

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…]

Posted in: General

Switch is just If-Else-If

April 24, 2008

0

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…]

Posted in: .NET, General, Tech/Hacks

On assigning null to value-types in VB.NET

April 16, 2008

0

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…]

Posted in: .NET, Tech/Hacks

Raising events from structures

April 14, 2008

3

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…]

Posted in: .NET, General

Tata Nano vs electric cars

April 10, 2008

0

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…]

Tagged: ,
Posted in: General

The empty try block mystery

April 10, 2008

17

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…]

Tagged: , ,
Posted in: .NET, General

How to check if a file is compressed in C#

April 8, 2008

2

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…]

Tagged: , ,
Posted in: .NET, General

IOException when trying to read a file using File.ReadAllBytes function

April 4, 2008

5

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…]

Tagged: ,
Posted in: General

WPF equivalent of InvokeRequired

January 10, 2008

8

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…]

Posted in: .NET, Tech/Hacks

Something about .NET string interning

December 11, 2007

0

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…]

Posted in: .NET, General, Tech/Hacks
Follow

Get every new post delivered to your Inbox.