Posted by Phil in .NET, .NET Examples
on Jun 18th, 2010 | Comments Off on Dealing with infinite recursion redux
In a previous post about infinite recursion I proposed the use of the ThreadStaticAttribute to deal with infinite recursion. This approach is fine so long as your recursion doesn’t cross threads, if it does the static variable will be reset for each new thread and offer not benefit.
Here’s an example, in .NET 4.0, to show you what I mean. In this example I have a simple logging setup, with a list of IListeners which handle messages that a logged with the LogMessage function. It’ll get into a infinite recursive loop becuase the LogToDiskListener attempts to log a message, and the...
Posted by Phil in .NET Examples
on Sep 14th, 2009 | Comments Off on Anonymous types that look the same, are the same
Anonymous types are one of the features that was introduced as part of .net 3.5, they allow a programmer to make a new type dynamically, with its properties defined from its usage.
An quick example of an anonymous type might be:
var anon = new { AnInteger = 1, AString = "string" };
Console.WriteLine(anon.AString);
Behind the scenes, the compiler will create a class that looks somewhat like this:
class Anon
{
public int AnInteger { get; private set; }
public string AString { get; private set; }
}
The type doesn’t look exactly like that, but from a usage point of view,...
Posted by Phil in .NET Examples
on Sep 3rd, 2009 | Comments Off on Locking on Value Types
Locking is a core principle when it comes to multi-threaded applications, it allows a program make sure a block of code is executed by only one thread at at time. The lock statement in C#, or SyncLock in VB.net, is a really useful command to quickly create a lock and guarantee that the lock is released when the code finishes, even if it throws an exception.
private static object _lockObject = new object();
private void SomeFunction()
{
lock(_lockObject)
{
// Do something
}
}
There’s one thing that’ll trip up people new to threading, which is that the .net runtime...
Posted by Phil in .NET Examples
on Aug 24th, 2009 | Comments Off on More thoughts on Lazy
Something that I missed when I mentioned my thoughts on an implementation of Lazy<T>, was that Microsoft was already a couple of steps ahead of me, and will include their own version in .NET 4.0, the MSDN documentation is here.
Their approach is slightly different to mine. Something I tried to do with my implementation was allow you to treat the Lazy<T> as the same type as the generic parameter, by adding the two implicit conversion operators and overriding the equals and hash methods. Microsoft has gone for a slightly different approach by making it immutable, and requiring you call the...
Posted by Phil in .NET Examples
on Aug 12th, 2009 | 2 comments
As part of some work to solve some problems I was experiencing with unclosed connections in WCF, I decided that on all calls to a method on a WCF interface, the channel should be closed immediately after use.
With the channels being generated by a ChannelFactory, I decided it would be best to write an implementation of the interface that simply re-implemented all the interfaces methods, with a using statement to close the channel, effectively creating a method like the following for every call.
public void SomeRemoteMethod()
{
var channel = GetChannel();
using((IDisposable)channel)
{
...