codeflood logo

Posts tagged in C#

Solving Generic Casting Issues with Actions

Generics in .net are a great way to avoid having to repeat your code when all you need to do is alter a few of the types involved in the code. Recently I was retrieving some objects from a data store in several different places in my code, and just altering the return types at each call. This felt like an ideal candidate for generics. I didn’t want to have to cast the array of objects to the...

Singleton Patterns for ASP.NET

Update 13th June 2011: Thanks to Damian Kulik who left a comment on this post pointing out that the classic singleton pattern I show below is not thread safe and provided a link to a great article showing a few options for creating a thread safe singleton in C#. I’ve updated the code examples for the classic singleton pattern to use a thread safe singleton pattern. Many of you are probably aware...

Calling Control

Several years ago I remember watching a dnrTV (dot net rocks TV) episode featuring Venkat Subramaniam discussing Ruby like features of .NET 2 and C# 2. You can catch the episode at http://www.dnrtv.com/default.aspx?showNum=41. One programming technique that stuck in my head from that episode was to do with passing control inside a method back to the calling code to allow it to fill in the specific...

Using Properties in Out Parameters in C#

Recently I was working on some DTO code that required setting a current value and an old value for a variety of properties on the DTO. The purpose of the DTO was to transfer changes to user details from inside Sitecore over XML to another system. But if a property hadn't changed then it shouldn't be populated into the DTO. And although the following is straight forward, it just felt like the code...