February 13th, 2009
By using pre-processor directives you can exclude parts of your code from being seen by the compiler. Excluded from the assembly they are never seen at run-time. This is different from a regular if (x) {} block where code is actually compiled in, evaluated at runtime and added to the assembly.
To understand why you would want to do this, a little history: One of the good things about C is that it works on every imaginable platform, the bad thing was of course that it works on every imaginable platform. There was always a little tweaking required to get your code to compile. Your program might have needed to compile on Amiga, DOS , OS/2, Windows or Linux. The invention of the preprocessor made this much easier. As a separate step prior to compilation it combs through the source code and modifies it according to the pre-processing instructions found in the source code. The C compiler never gets to see the things that don’t apply to it.
Read the rest of this entry »
Tags: c#, language, preprocessor directives
Posted in Beginner, Learn C# | No Comments, yet!
February 13th, 2009
Very often you need to change part of a string, maybe just once, or many times over. Strings in .NET/C# are immutable we cannot actually change a string in-place. But we are able to work on copies. The code example below attaches two new methods to the C# string class.
- The ReplaceFirst method replaces the first occurrence of “needle” in a string and replaces it with “replacement”.
- The ReplaceAll function is similar: it steps through the string modifying it each time it finds “needle” and replaces it. To avoid a possible infinite loop it first checks whether “needle” is equivalent to “replacement”.
Read the rest of this entry »
Tags: c#, regex, strings
Posted in Beginner, Learn C# | 3 Comments - getting there! »
February 11th, 2009
A common programming problem is to find the position of all copies of a string in another string. For finding the first copy the C# string method IndexOf is similar to the C strpos() function. It returns the first occurrence of a string in another string. But what if you would like to find the position of all occurances of the substring? The following “IndexOfAll” method does just that. It returns an IEnumerable containing the offsets of each sub-string in the main string.
Read the rest of this entry »
Tags: c#, extension, strings
Posted in Beginner, Learn C# | 5 Comments - getting there! »
February 10th, 2009
With the introduction of .NET 3.5 C# includes the “var” keyword to support anonymous types. One important motivation for this was to make code written with LINQ (Language-Integrated Query) easier to read. So what is an anonymous type? Anonymous types simply mean that you don’t specify the type — but let the compiler do it instead.
Read the rest of this entry »
Tags: anonymous, c#, language, LINQ
Posted in Beginner, Learn C# | No Comments, yet!
February 5th, 2009

Whole programming languages have been designed (*cough* perl) so that we can cut delimited strings into bits and string them back together. For this purpose C# provides the String.Split() and String.Join() functions. You specify how you would like to split or merge the string and they do the work. In this post we look at some common example uses and then put together a simple CSV (comma separated values) parser.
Read the rest of this entry »
Tags: csv, strings
Posted in Beginner, Learn C# | No Comments, yet!
February 4th, 2009

String.Format is a very powerful method but the documentation at MSDN is quite wordy and spreads the details over many pages. For this post I have made quick reference to its many specifiers and for good measure added some examples to help get you started.If you would like to format a date, currency, number or just the time, String.Format is your friend.
Read the rest of this entry »
Tags: Learn C#, string.format
Posted in Beginner, Learn C# | 2 Comments - getting there! »
February 3rd, 2009
If you are interested in turning a C# string into an integer, float, double or any other kind look no further. In todays post we make use of the convert and parse methods to convert strings into numbers. Read the rest of this entry »
Tags: converting, float, integer, string
Posted in Beginner, Learn C# | No Comments, yet!
January 7th, 2009
As your application grows it can be useful to get an idea of how much memory a particular data structure is using in memory. Measuring memory in a garbage collected environment is a somewhat of a moving target as the Garbage collector is able to move things around in the background.The .NET libraries offer two ways of measuring your applications memory. The total amount of memory allocated to the process (which includes code, unique libraries) and the amount of memory used that the Garbage collector is aware off (which is only your applications variables). This second set is of course a subset of the total amount of memory.
Read the rest of this entry »
Tags: garbage collector, hack, Learn C#, memory usage, object creation, process
Posted in Intermediate, Learn C# | 1 Comment already!»
January 7th, 2009

Occasionally you need to time the performance of a C# function to see if its as fast, or as slow, as you think it is. You could of course run your code through a profiler. But in situations that that would be overkill there exists a quick solution. Originally I looked at the standard DateTime class, but NET 2.0 introduced theĀ Stopwatch class which is much more suitable. In this post I look at how you can measure your functions performance down to the last fraction of a millisecond.
Read the rest of this entry »
Tags: datetime, function execution, Learn C#, stopwatch, timing
Posted in Beginner, Learn C# | 1 Comment already!»
January 6th, 2009
The .NET C# library provides all the basic elements for encrypting a string with a passphrase and decrypting it later. Doing this however requires a few steps in between. This post show a simple set of routines to help you do just that. We use the TripleDES encryption suite to do the actual encryption, with a little help from the MD5 hash sum generator.
The complete source code is listed below, but lets have a little look at how it works first.
Read the rest of this entry »
Tags: decryption, encryption, Learn C#, md5, strings
Posted in Intermediate, Learn C# | 13 Comments - getting there! »