Blog LogoBlog Logo

Tag: C#

All tags

Fixing out of sequence timestamps

I maintain a small open source project that helps test log message . Part of this is that each log message has a sequence number and timestamp attached to it....

— 25 Feb 2025

Why is there a difference between decimal 0 and 0.0?

const decimal ZeroA = 0M; const decimal ZeroB = 0.0M; They're the same thing, right? Well, almost. The equality operator says they're the same thing. bool...

— 03 Jul 2021

ReSharper test runner still going after tests complete

I've been writing some tests and I got this message: [RIDER LOGO] Unit Test Runner The process ReSharperTestRunner64:26252 has finished running tests assigned...

— 10 Oct 2020

Ensure Controller actions/classes have authorisation

A couple of years ago I wrote a unit test to ensure that all our controller actions (or Controller classes) had appropriate authorisation set up. This ensures...

— 17 Aug 2018

Creating a Throttle with an ActionBlock - Addendum (Cancelling)

In my previous post I described how to create a throttle with an action block so you wouldn't have too many tasks running simultaneously. But what if you want...

— 17 Jul 2018

Creating a Throttle with ActionBlock

We have an application that needs to perform a repetitive task on many external services and record then aggregate the results. As the system has grown the...

— 16 Jul 2018

Paramore Brighter: The Russian Doll Model

I've mentioned a bit about the attributing the handler and the step and timing parameters, but I've not explained them properly in previous posts (" Retrying...

— 31 Mar 2018

Paramore Brighter: DRY with Custom Decorated Command Handlers

You may wish to add similar functionality to many (or all) command handlers. The typical example is logging. You can decorate a command handler in a similar...

— 18 Mar 2018

Paramore Brighter with Quality of Service: Retrying Commands

Paramore Brighter supports Policies to maintain quality of service. This is useful when your command makes calls to external services, whether they are...

— 02 Mar 2018

Paramore Brighter: Implementing a fallback exception handler

So far in this series, we have a basic command processor set up and able to dispose of resources . But what happens when things go wrong? The command processor...

— 16 Feb 2018

Paramore Brighter: Ensuring Dependencies are Disposed.

In my previous post , I showed how to set up Paramore Brighter with the built in Dependency Injection provided with .NET Core 2. However, it wasn't the full...

— 05 Feb 2018

Paramore Brighter: Using .NET Core's Dependency Injection

This is the first in a series of posts on Paramore.Brighter . I'm writing this as a series of recipes, with the aim of you picking up a point quickly and...

— 02 Feb 2018

Join Null Check with Assignment

I recently wrote some code and asked ReSharper to add a null check for me, which it did. Then it suggested that I could simplify the null check by joining it...

— 16 Jul 2017

Round Robin class

We recently had need of a round robin functionality and since there is no round robin class built into .NET I needed to build my own class. It is a fairly...

— 24 Sep 2016

Overusing the Null-Conditional Operator

The null-conditional operator is the ?. between object and field/property/method. It simply says that if the thing on the left hand side is null then the thing...

— 10 Sep 2016

Debugging a process that cannot be invoked through Visual Studio.

Sometimes it is rather difficult to debug through Visual Studio directly even although the project is right there in front of you. In my case I have an...

— 31 Aug 2015

The difference between & and && operators

Here is a bit of code that was failing: if (product!=null & !string.IsNullOrEmpty(product.ProductNotes)) If you look closely you can see it uses just the...

— 11 Jun 2015

Code Review: Making a drop down list out of an enum

I’ve come across code like this a couple of times and it is rather odd: IEnumerable<CalendarViewEnum> _calendarViewEnums =...

— 20 Apr 2015

Code Review: FirstOrDefault()

I regularly review the code that I maintain. Recently, I've come across code like this fairly often: someCollection.FirstOrDefault().Id I cannot rightly...

— 09 Apr 2015

A better tracing routine

In .NET 4.5 three new attributes were introduced. They can be used to pass into a method the details of the caller and this can be used to create better trace...

— 20 May 2014

Using Contracts to discover Liskov Substitution Principle Violations in C#

In his book Agile Principles, Patterns, and Practices in C# , Bob Martin talks about using pre- and post-conditions in Eiffel to detect Liskov Substitution...

— 20 Apr 2014

Checking for NULL using Entity Framework

Here is a curious gotcha using the Entity Framework: If you are filtering on a value that may be null then you may not be getting back the results you expect....

— 22 Nov 2011

Handling bounces on Amazon SES

If you send to an email that does not exist, Amazon SES will perform some handling of the bounce before passing the details on to you. When you send email...

— 18 Nov 2011

Verifying Senders with Amazon SES

I’ve already written a couple of pieces about Amazon Simple Email Service (SES) on sending Email and sending emails with attachments . Why do you have to...

— 13 Nov 2011

Sending more than a basic email with Amazon SES

Previously, I wrote about getting started with Amazon’s Simple Email Service , and I included details of how to send a basic email. The SendEmail method is...

— 10 Nov 2011

First(OrDefault) Vs. Single(OrDefault)

There are two mechanisms (each with an …OrDefault variant) in LINQ for getting one item out of an enumeration. They are First and Single . There is a...

— 07 Oct 2011

Tip of the day: Splitting a string when encountering whitespace

In .NET the string class has a Split method that splits the string at the separator character(s) that you specify. However, if you want to split the string at...

— 08 Jun 2011

Building messages in parallel

I recently saw some code where the developer was attempting to build up messages inside tasks that were being reported outside of the task. In a sequential...

— 25 May 2011

Parallel Tasks and the HttpContext

A few days ago I spotted a question on StackOverflow by someone trying to use a parallel loop in an ASP.NET application. It may have been an ASP.NET MVC...

— 24 May 2011

ASP.NET MVC – Pretty URLs

As the little Harris Benedict Calculator application stands , the only way to get some sort of answer out of it is to fill in the form. What if you wanted to...

— 15 May 2011

ASP.NET MVC 3 – Introduction to validation

In my previous post on MVC 3 I started a project to calculate a calorific intake required to maintain a stable weight. In this post I’ll extent that to add...

— 14 May 2011

ASP.NET MVC – Server Side Validation

So far, we’ve built up a basic application and got some client side validation working. However, client side validation only goes so far. While it can prevent...

— 14 May 2011

Starting an ASP.NET MVC 3 application

In this post, I’m going to show the basics of starting an application with ASP.NET MVC 3. The demo application will be a simple calorie counter that takes in a...

— 13 May 2011

Custom error pages and error handling in ASP.NET MVC 3

In ASP.NET MVC 3 a new bit of code appeared in the global.asax.cs file: public static void RegisterGlobalFilters(GlobalFilterCollection filters) {...

— 02 May 2011

Tip of the day: Obtaining all subdirectories recursively

This is an example of how to obtain a list of all subdirectories using a recursive method with the .NET Framework. public static List<DirectoryInfo>...

— 22 Apr 2011

Parallelisation Talk Example - Aggregate Exceptions

The two code examples here show what happens when exceptions are thrown within tasks that are not handled within the task. In each case the task that has the...

— 21 Apr 2011

Parallelisation Talk Example – ConcurrentBag

This example shows a ConcurrentBag being populated and it being accessed while another task is still populating the bag. The ConcurrentBag class can be found...

— 21 Apr 2011

Parallelisation talk example – Independent Object Graphs

Parallelised code works best when data is not shared. This example shows a simple piece of parallel code where each task operates independently on its own...

— 21 Apr 2011

Parallelisation Talk Example – Parallel.Invoke

Parallel.Invoke is the most basic way to start many tasks as the same time. The method takes as many Action<…> based delegates as needed. The Task Parallel...

— 21 Apr 2011

Parallelisation Talk Examples – Basic PLINQ

These are some code examples from my introductory talk on Parallelisation showing the difference between a standard sequential LINQ query and its parallel...

— 21 Apr 2011

Parallelisation Talk Examples – ConcurrentDictionary

The example used in the talk was one I had already blogged about. The original blog entry the example was based upon is here: Parallelisation in .NET 4.0 – The...

— 21 Apr 2011

Parallelisation Talk examples - Parallel.For

This is some example code from my introductory talk on Parallelisation. Showing the difference between a standard sequential for loop and its parallel...

— 21 Apr 2011

Parallelisation Talk Examples – Parallel.ForEach

These are some code examples from my introductory talk on Parallelisation. Showing the difference between a standard sequential foreach loop and its parallel...

— 21 Apr 2011

Prallelisation Talk Example – Tasks within Tasks

In this example I’m showing the launching of further tasks within an existing task. The Main method launches a single task (of course, it would likely be many...

— 21 Apr 2011

Building a tag cloud with LINQ

I have a set of blog posts that I’m representing as a List of BlogPost objects. A BlogPost is class I created that represents everything to do with a blog...

— 02 Apr 2011

Tasks that create more work

I’m creating a program that parses a web page then follows the links and then parses the next set of web pages to eventually build a picture of an entire site....

— 01 Apr 2011

Using semaphores to restrict access to resources

I’m in the process of building a small data extraction application. It uses the new Parallel Extensions in .NET 4 in order to more efficiently extract data...

— 30 Mar 2011

Parallelisation in .NET 4.0 - The concurrent dictionary

One thing that I was always conscious of when developing concurrent code was that shared state is very difficult to deal with. It still is difficult to deal...

— 24 Mar 2011

A quick intro to the HTML Agility Pack

I want a way to extract all the post data out of my blog. To do that I’m building a little application to do that, mostly as an exercise to try out some new...

— 22 Mar 2011

Parallelisation in .NET 4.0 - Part 2 Throwing Exceptions

With more threads running simultaneously in an application there is increasing complexity when it comes to debugging. When exceptions are thrown you usually...

— 14 Feb 2011

Parallelisation in .NET 4.0 - Part 1 looping

In an upcoming project we have a need for using some parallelisation features. There are two aspects to this, one is that we have to make multiple calls out to...

— 08 Feb 2011

Updated chart of .NET version numbers

Way back when, I published a table detailing the version numbers of the various parts that make up a .NET application: The Tools, the C# language, the...

— 17 Mar 2010

Tip of the Day: Duplicate input fields

Don't allow duplicate input fields into your form. The other day I was trying to debug a bug in an application that I maintain. The code created a set of...

— 06 Feb 2010

Tip of the Day: NaN (Not a Number)

The Issue If you want to detect if a double (System.Double) or float (System.Single) is 'not a number' or NaN you cannot use something like this: if (myDouble...

— 12 Sep 2009

Tip of the day - String Equality

When comparing two strings in a case insensitive manner, use: myFirstString.Equals(mySecondString, StringComparison.InvariantCultureIgnoreCase) or, if cultural...

— 12 Jul 2009

Method hiding or overriding - or the difference between new and virtual

When developing applications it is very important to understand the difference between method hiding and method overriding. By default C# methods are...

— 10 Oct 2008

The try-catch-return-false-throw-catch-return-false-throw-fail anti-pattern

I recently came across a wonderful anti-pattern. Well, anti-patterns are not wonderful, but the mind just boggles at the sheer bloody lunacy of this particular...

— 29 Aug 2008

Mixins in .NET (again)

A while ago I wrote about Mixins in C# 3.0 , at the time I was saying that you can get some of the functionality, but not all, from some of the new language...

— 10 Aug 2008

Crazy Extension Methods Redux (with Oxygene)

Back in April I blogged about a crazy thing you can do with extension methods in C#3.0 . At the time I was adamant that it was a bad idea. I still think it is...

— 07 Aug 2008

Monitoring change in XML data (LINQ to XML series - Part 5)

This is the 5th part in a series on LINQ to XML. In this instalment we will look at monitoring changes in XML data in the XML classes added to .NET 3.5. The...

— 19 Jul 2008

Tip of the Day #4 (Connection Strings in Config files)

From .NET 2.0 onwards a new and improved configuration management system has been put in place. You can now add a <connectionString> element to the config file...

— 19 Jul 2008

Navigating XML (LINQ to XML series - part 4)

In my last few posts on LINQ to XML ( part 1 , part 2 and part 3 ) I've shown you a starter on navigating around XML data. In this post I'll continue to show...

— 22 Jun 2008

Navigating XML (LINQ to XML series - Part 3)

In my last two posts I've been introducing you to the the new XML classes in .NET 3.5. In this post I'll continue that and show you some...

— 18 May 2008

Crazy Extension Method

Here is an example of a crazy extension method that alters the semantics of method calling. First the extension method: public static class MyExtensions {...

— 16 Apr 2008

Fizz-Buzz in LINQ

This just occurred to me. It is somewhat pointless, but I thought it was interesting: static void Main( string [] args) { var result = from say in Enumerable...

— 16 Apr 2008

Getting values out of XML in .NET 3.5 (LINQ to XML series part 2)

In my last post I gave a brief introduction to some of the new XML classes available in .NET 3.5. In this post I'll continue that introduction by explaining...

— 12 Apr 2008

Introduction to LINQ to XML

Last year I wrote about the new languages features available in C# 3.0 ( Anonymous Types , Extension Methods , Automatic Properties , A start on LINQ , Object...

— 08 Apr 2008

Mixins in C# 3.0

This is something I've been mulling around in my head for a few days now. "Out of the box" C# 3.0 does not support mixins, but I think you can get some of the...

— 24 Feb 2008

The value of smaller methods

A while ago on a forum I advised someone that their method was too long and difficult to read and maintain. I suggested that they break the code into smaller...

— 30 Dec 2007

What is a DAL (Part 4)

As has been mentioned previously, one of the purposes of the DAL is to shield that application from the database. That said, what happens if a DAL throws an...

— 15 Oct 2007

What is a DAL (Part 3)

In this continuation of my series on the DAL I'm going to show the ability to create several DALs and have a Factory class instantiate the correct DAL based on...

— 14 Oct 2007

What is a DAL?

I occasionally see posts on forums asking for help with some database problem. Then I see they've been using the the wizards in Visual Studio (which is okay...

— 28 Aug 2007

Things I keep forgetting about FileInfo

This is going to sound like a real newbie post. But I keep forgetting this particular bit of information and I keep having to write little throw away...

— 23 Jun 2007

The simplicity of nullable types

I just discovered nullable types. Wow! They are really simple and such a powerful feature. Just see for yourself.... If you have an int or a DateTime or any...

— 22 Jun 2007

The Conditional attribute in .NET

One of the odd thoughts that shoot across my brain today was how does Debug.Assert() work? I mean there are not separate debug and release builds of the .NET...

— 20 Jun 2007

A start on LINQ

I was at the Microsoft MSDN Roadshow today and I got to see some of the latest technologies being demonstrated for the first time and I'm impressed. Daniel...

— 18 Jun 2007

Anonymous Types

Anonymous Types are another new feature to the C# 3.0 compiler. To create one, just supply the new keyword without a class name, followed by the, also new,...

— 18 Jun 2007

Automatic Properties

Continuing my look at the new features found in the C# 3.0 compiler I will look at Automatic Properties. public class Employee{ public string FirstName { get;...

— 18 Jun 2007

Extensions Methods

There have been many times a class has needed to be extended, but the additional code just doesn't sit right on the class itself, or I don't have access to the class...

— 18 Jun 2007

Object Initialisers (1)

Continuing with the language enhancements in C#3.0. This post presents the concept of object initailisation. Say you have a class with a default constructor...

— 18 Jun 2007

Object Initialisers (2)

Following on from a comment left on my previous post on Object Initialisers, I decided to take a screenshot of Orcas displaying the tooltips. I...

— 18 Jun 2007

Object Initialisers (3)

It seems that now I've got Lutz Roeder's Reflector on the case with Orcas the way object initialisers work slightly different to how I expected. As it was...

— 18 Jun 2007

Test Driven Development By Example

Introduction A lot has been written on the subject of test driven development, and especially on the idea that tests ought to be written first. This is an...

— 28 Mar 2007

How to get a list of all subdirectories

This is an example of how to obtain a list of all subdirectories using a recursive method with the .NET Framework. public static List...

— 04 Jul 2006

Passing Values Between Forms in .NET

Introduction I wrote this article in response to an almost overwhelming number of requests on forums on how to pass a variable from a one Windows Form to...

— 22 Apr 2005

The Lazy NotSupportedException

I am currently building a class library that makes extensive use of abstract classes and interfaces, and like all good TDD (Test Driven Development) I am writing the tests up front...

— 19 Sep 2004