Blog posts with tag "csharp"

Object-oriented theory you may not care or know about, but are doing

October 04, 2016

The law of demeter A method in a should not send a message to another object unless that object is one of the following: An instance…

Do you write unit tests before code, or after?

May 05, 2016

One of the biggest omissions in the TDD literature is the concept of code first vs test first. The discussion doesn’t seem to surface very…

C# Unit test generator

April 24, 2016

This small Javascript pen generates a C# file for an object you want to test, using a list of test cases you specify in the textbox in this…

Unit test naming conventions

November 04, 2015

###What’s in a name? Uncle Bob has written on this topic at great length in his books x and y. He has some great tips on naming methods, but…

.NET logging libraries compared

August 08, 2014

This is a short presentation I created for the company I work for, and gives my thoughts on the state of .NET logging libraries in 2014. My…

A C# UK bank holiday calculator

May 21, 2014

In the UK we’re fortunate enough to get 5 free holidays a year, under the guise of “Bank Holidays”. These are mostly on Mondays and come in…

Introduction to Genetic Algorithms in C#

May 14, 2013

Get the code A long time ago I mentioned in this post that I was planning on writing up some notes I made at university about Genetic…

How to Write a Spelling Corrector in C#

April 05, 2013

Github repository - feel free to send improvements Peter Norvig’s spelling corrector is fairly famous in nerd-circles as it describes the…

Moqs versus Stubs (2013 edition)

February 25, 2013

This is an age old debate which I’ll chirp in with my opinion. According to Gojko Adzic there’s two types of TDD people in the world…

Serializing an object to XML snippet

March 14, 2012

I’ve written this same snippet so many times I’ve decided to shove it here to save my fingers a few calories in future. It’s nothing special…

Filtering System.Diagnostics Trace messages

February 13, 2012

One of the biggest points earners for me on Stackoverflow has been this question about Error logging in C#/.NET. Having been fairly…

Parsing XFDF (PDF annotations) in C#

February 07, 2012

I’m in the middle of doing the final two modules of the 3rd year of my part-time Computer Science degree, which means going back to the…

Genetic Algorithms in C#

February 21, 2011

The code below is a 3.5 upgrade to Barry Lapthorn’s original C# example found on codeproject. I’ve .NET’ised it a bit more (inline with the…

C# Bit manipulation by example (part 2)

February 02, 2011

The previous post explained: Counting and arithmetic in base 2,8,10,16 (binary, octal and decimal,hexadecimal), Sign magnitude 1/2s…

Linked list and Double linked list in C#

December 22, 2010

Below are two examples of implementing a linked and double linked list in C#. The framework already has a LinkedList implementation from…

C# Bit manipulation by example (part 1)

December 22, 2010

This is the first part of two blog posts trying demystify bit manipulation in C/C#. Below are several topics that I found useful background…

6 ways to get the current directory in C#

August 17, 2010

Small update: added the dotnet core way of doing things (Directory.GetCurrentDirectory()) System.AppContext.BaseDirectory This is the…

Password Generator

August 16, 2010

Update: It’s 2015 and no browser supports Silverlight anymore (good riddance). Feel free to compile the source and turn it into a console…

CommandOptions - interactive console application command parser

August 08, 2010

This is a class based on the Novell Options class for easily parsing commands and their arguments for console applications that sit and wait…

Extracting all links from a HTML page

July 30, 2010

The code below is a small class that extracts all links from a HTML page using a regular expression. The method returns a list of URLs…

An extension method for converting a byte array to a string (reading its BOM)

July 19, 2010

Below is an extension method that adapts Rick Strahl’s code (and the comments too) to stop you having to guess or read the byte order mark…

Silverlight JSON WebClient wrapper/helper

June 06, 2010

This is a small class that uses the NewtonSoft JSON.NET library to make JSON requests to a URL. It handles both requests that require…

Delegates, Funcs and Actions by example

May 03, 2010

This is a small set of reference examples of the various ways of declaring delegates, funcs and actions across the different versions of the…

Listing Windows users and groups in C#

April 16, 2010

This example is borrowed from a newsgroup post, apologies to whoever should take the credit but I lost the original url. It’s a basic…

Syntax highlighter for C#

April 07, 2010

This is small class for color coding, syntax highlighting, pretty printing, prettifying (any of the above) C# source code. It produces HTML…

ApplicationException versus Exception

April 05, 2010

Exceptions The two often repeated phrases about Exceptions in .NET is they should be used for “exceptional behaviour” and sparingly; they…

Using Google maps with C#

March 16, 2010

Download example solution This example/download was made to retrieve all UK postcodes from a webpage you give, and then plot them onto…

String.Equals(), == and String.Compare()

March 02, 2010

Most C# books will tell you from the early chapters that you should always override Equals in your class instead of relying on the base…

Using XmlTextWriter to produce a XML string

February 16, 2010

This is a small snippet showing an example of using a XmlTextWriter to produce Xml that validates. One point to note is that because .NET…

Other Design Patterns

February 08, 2010

Here are some remaining software design patterns I missed out from the set of examples I gave. Adapter A good example of the Adapter pattern…

Simplified C# Atom and Rss feed parser

February 08, 2010

.NET already has quite a few open source RSS and ATOM libraries for parsing feeds. The most complete one is Argotic but there are also 3-…

IConfigurationSectionHandler example

January 01, 2010

This is a small snippet for the basics of writing a ConfigurationHandler to read a configuration section from your web.config or app.config…

Value types, reference types, boxing and unboxing in C#

December 14, 2009

No boxing topic is complete without a quick summary of value types and reference types: Value types Stored on the stack (STAtiC types are…

Embedded resources example

November 15, 2009

This is a small example showing the discover and reading of all embedded resources in your assembly. On a slight but vaguely related tangent…

Abstract methods vs Virtual methods

October 20, 2009

Many moons ago I was curious about the difference in IL that is produced between abstract and virtual methods, and this is the result. What…

Export to Excel in C#

October 12, 2009

This is a very simple snippet showing how to use the Microsoft.Office.Interop.Excel assembly (version 12 for this example).

LDAP/Active directory tree in C#

October 12, 2009

Download This winforms application from 2004 shows the basics of querying an Active Directory and LDAP server, including OpenLDAP, Netscape…

Equals vs IEqualityComparer, IEquatable<T>, IComparable, IComparer

September 27, 2009

This is a look at the difference between Equals vs IEqualityComparer, IEquatable, IComparable, IComparer which are used for sorting and…

Format XML in C#

September 09, 2009

Below is a small snippet showing how to format (or re-format) XML so it’s indented. XML isn’t stored in this humanly readable way in…

3 ways to leave your exception

September 08, 2009

Below is a small reference showing, by example, the 3 ways of throwing/re-throwing exceptions in C#, and their outcomes.

Const vs Readonly in C#

September 06, 2009

Const The value of your const property is set at compile time and can’t change at runtime. Where the const is used in the callee, this value…

C# Symmetric encryption wrapper

July 24, 2009

The original class for this post can be found at codeproject.com. This class is a tidied up version (and disposes more aggressively). You…

GZip and Deflate page compression in ASP.NET

July 13, 2009

Download Having written a GZip compression class in PHP many moons ago, I was interested to find out if anyone had done something similar in…

Objective C by example for a C# developer

June 22, 2009

This is a small set of code snippets that may assist as a quick reference when switching between C# and Objective-C concepts. It’s all…

Wake up from Sleep (CreateWaitableTimer) in C#

June 18, 2009

This code isn’t mine, but from a newsgroup posting on Eggheadcafe.com by MVP Willy Denoyette (I couldn’t find a homepage to link). I’m…

Command line arguments parser

June 17, 2009

There are already 2 or 3 command line arguments in C#, two of which are found on the codeproject.com website. Both of these didn’t match my…

Dynamically compiling XSL in C#

June 07, 2009

If you use XSL extensively in any .NET applications then you will probably find yourself encountering performance issues with your XSL in an…

Your base class: Abstract or Interface?

May 18, 2009

This could be seen as a bit of a beginner’s topic, but one thing that is often overlooked by the tutorials and books is deciding when to use…

C# Design Patterns: the Façade pattern

April 14, 2009

Summary Access multiple classes in one simple to use class. This class is often static too. Example A common use of the Façade pattern is…

Image conversion in C#

April 13, 2009

Download This old application from 2004 demonstrates image conversion using the built in classes in the BCL. Like all the old C# code here…

Automated browser screenshot capture

March 18, 2009

BrowserCapture.exe.zip Project source (Visual Studio 2008) This small windows forms application does the following: Opens a website you…

Pronounceable password generator

March 15, 2009

This is the source code for the pronounceable generator from the password generator page. The alpha-numeric password generator is very…

C# Design Patterns: the State pattern

March 09, 2009

Summary Like a finite state machine but implemented using a class for each state, rather than an enumeration and/or switch statement…

Friendly Unique Id Generation part 2

March 05, 2009

Part 1 Base 64 Guid Part 2 Numeric Alphanumeric Host Time Ticks MD5 Pronounceable password Base62 2 random numbers This displays the id in…

Friendly Unique Id Generation Part 1

March 03, 2009

Part 1 Base 64 Guid Part 2 Numeric Alphanumeric Host Time Ticks MD5 Pronounceable password Base62 URL shortening: Tinyurl http://tinyarro.ws…

C# Design Patterns: the Observer pattern

February 28, 2009

Summary Very similar to the Mediator pattern. The only difference is the ‘clients’, or the Observers as they are now called, don’t broadcast…

Output from the Uri class

February 27, 2009

This shows the output from all the properties of the URI class for the current request. The Uri has been hardcoded as:

C# Design Patterns: the Mediator pattern

February 10, 2009

Summary This pattern is best illustrated in code as the source describes it a lot more concisely than in prose. One central class (the…

C# and .NET type visibility

February 03, 2009

Classes and Structs Classes and structs allow the following type visibility options in C#: public Viewable by all types in the current and…

C# Design patterns: the Strategy pattern

January 28, 2009

Summary Multiple classes implement an interface, handling an algorithm in a different way. Example Sorting is the clearest working example…

C# Design Patterns: the Iterator pattern

January 23, 2009

Summary Iterate through a list of items, with an enumerator class ordering the set/list before it is iterated through. Example Iterators are…

C# MD5 and SHA encryption wrapper class

January 14, 2009

This is a simple utility class for MD5 (128bit) or SHA-2 (256bit,384bit and 512bit) hash encryption. It outputs the string using 2 byte hex…

C# Design Patterns: the Singleton pattern

January 13, 2009

Summary A class that only allows one instance of itself, which is accessed via a static property. Example usage The commonest example is an…

C# Design Patterns: the Abstract Factory Pattern

January 02, 2009

Summary The abstract factory is the base class of all factory classes, but is responsible for creating instances of the classes that are…

C# Design Patterns: the Factory pattern

December 03, 2008

Summary One class (the factory) creates instances of other classes which all implement the same interface or subclass a specific class. The…

Whois, DNS, CName, MX record lookup in C#

October 06, 2008

Download This is the source code from the network tools page that use to exist on the previous domain (when the site was hosted on a…

Sending messages to windows in C# via winapi

September 16, 2008

Download This windows demo app from 2004 shows you how to use the winapi EnumWindows function in association with SendMessage to send…

C# Design Patterns

September 02, 2008

Contents Factory Abstract Factory Singleton Strategy Iterator Mediator Observer State Facade The others Introduction Download example…

Serialization as a database

August 05, 2008

Download This app from 2004 demonstrates serialization in its simplest form, in particular for use as a storage medium. The demo creates 2…

XML Browser (demo app)

February 05, 2008

Download This simplistic demo app from 2004 works as a simple XML browser, showing all XML nodes in an XML document in a treeview, the value…

Streaming binary/text data via the web in C#

May 24, 2007

Download This rough example was originally intended for auto-update functionality in an application (it downloads the new .exe, runs a…

Binary upload to SQL Server in

February 24, 2006

Download This tool from 2004 is useful if you need to do a quick update of a binary column in a SQL server 2000 table. It could be easily…

Changing a Richtextbox caret

February 16, 2006

Download This old bit of code from 2004 is an unfinished (i.e. not that pretty to read) demo project for changing the caret in a rich text…