New Async Programming Videos

Some great new video’s on MSDN showing how to do async programming using the Async CTP. There are different versions of video’s for both VB and C#. This is a great opportunity to see the power of the new Async languages featuresand follow through a series of examples showing how easy it is to… Read more

How to keep a local variable in scope across a try and catch block?

The following code won’t work, because conn goes out of scope before you enter the catch block.        try         {             Connection conn = new Connection();             conn.Open();         }         catch         {             if (conn != null) conn.Close();         }   The fix is simple – just declare conn before entering the… Read more

Where can I get a full comparison between C# and VB.NET?

Microsoft provides a very full language equivalents page which compares not only C# and VB.NET, but also other languages targeted at the .NET framework. It looks at the equivalent concepts, keywords, types, operators etc. A very valuable resource when you’re trying to read or write code in a language which isn’t your preferred one. [Author:… Read more

What do I use instead of addressof?

To create delegate instances in C#, you just specify the delegate type, the method, and (if you want to create a delegate targetting a different instance or type from the current one) the target. For instance, each of these creates a ThreadStart delegate: ThreadStart x1 = new ThreadStart(SomeInstanceMethod); ThreadStart x2 = new ThreadStart(AnotherType.SomeStaticMethod); ThreadStart x3… Read more

How do I get the rightmost part of a string, as with the VB Right function?

Use <code>String.Substring</code>. Assuming that <code>x</code> is a string of length at least <code>n</code>, to get the last <code>n</code> characters, you would use <code>x.Substring(x.Length-n)</code>. <p> Note that the above assumes that the string is at least <code>n</code> characters long. For a more robust version, you might use something like: <code>x.Length . </code> [Author: Jon Skeet]… Read more

Why doesn’t C# have VB.NET’s ‘with’ operator?

Many people, including the C# language designers, believe that ‘with’ often harms readability, and is more of a curse than a blessing. It is clearer to declare a local variable with a meaningful name, and use that variable to perform multiple operations on a single object, than it is to have a block with a… Read more

What are the advantages of C# over VB.NET and vice versa?

The choice between C# and VB.NET is largely one of subjective preference. Some people like C#’s terse syntax, others like VB.NET’s natural language, case-insensitive approach. Both have access to the same framework libraries. Both will perform largely equivalently (with a few small differences which are unlikely to affect most people, assuming VB.NET is used with… Read more