Deterministic Finalization with IDisposable and the Using Statement

,
October 14, 2016 / No Comment

The problem with finalizers on their own is that they don’t support deterministic finalization (the ability to know when a finalizer will run). Rather, finalizers serve the important role of being a backup mechanism for cleaning up resources if a developer using a class neglects to call the requisite cleanup code explicitly.
For example, consider the TemporaryFileStream, which includes not […]
Read more

AUTHOR: CATEGORY: .Net, Blog, C#

Locking a Block of Code With an Integer

,
June 27, 2016 / No Comment

I was talking with a developer recently who was convinced that you could use a static integer variable to lock a block of code merely by casting it to an object, like this

First it must be noted that an integer cannot be used by itself in a lock statement because it is a value type. But when you […]
Read more

AUTHOR: CATEGORY: .Net, Blog, C#

Guidelines for Exception Handling in C#

,
September 14, 2015 / 1 Comment

The following is a list of C# Exceptions Handling Guidelines taken from Essential C# by Mark Michaelis.
Exception handling provides much-needed structure to the error-handling mechanisms that preceded it. However, it can still lead to some unwieldy results if used haphazardly. The following guidelines offer some best practices for exception handling.

Catch only the exceptions that you can handle.
Read more

AUTHOR: CATEGORY: .Net, C#

IntelliTect Presents at the Seattle Code Camp 2012

,
June 15, 2012 / No Comment

Asynchronous Programming with C# 5.0 – Mark Michaelis
In C# 5.0 there is really only one significant syntax addition – support for asynchronous programming that is embedded into the language. In this session we go over the is and is-nots of what C# 5.0 enables in the way of parallel processing – both for the UI and for the server […]
Read more

AUTHOR: CATEGORY: .Net, C#, Headlines, PowerShell

Parallel.For where i does not go to i++

,
April 25, 2010 / 1 Comment

A problem was recent posed that I hadn’t thought to do using Parallel.For in C#. How do you parallelize a for loop variable increased by something other than i++? Typically, I would simply iterate over a enumerable collection and run against the data in parallel using Parallel.For and Parallel.ForEach.

It turns out that there is no support for this in the PFX API. Parallel.For does not support incrementing other than one and it does not support reverse iteration. Also, Enumerable is a static class – which means we can’t do extension methods on Enumerable. However, we can use a custom iterator to generate the sequence for us. By using a Func<int, int> allows us to use our familiar i+= syntax for the increment function. We can quickly create a very simple iterator that will help us out.

AUTHOR: CATEGORY: .Net, Headlines, LINQ