Home About

 February 13th, 2009

C# Preprocessor Directives Explained - 0

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 »

 February 13th, 2009

Manipulating Strings in C# -Replacing part of a string / Replacing all occurences of a sub-string - 2

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 »

 February 11th, 2009

Manipulating Strings in C# – Finding all occurrences of a string within another string - 2

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 »

 February 10th, 2009

C# Anonymous types: the Basics - 0

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 »

 February 5th, 2009

Using String.Split and String.Join to build a simple CSV reader and writer in C# - 0

Creating Delimited Strings in C#
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 »

 February 4th, 2009

Formatting Strings in C# with String.Format - 1

A String.Format Reference for C#

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 »

 February 3rd, 2009

Converting C# strings to other types - 0

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 »

 January 7th, 2009

Measuring memory usage of object creation in C# - 1

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 »

 January 7th, 2009

Timing C# function performance with the Stopwatch class - 1

Timing C# function execution time with a stopwatch
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 »

 January 6th, 2009

Encrypting and Decrypting a C# string - 1

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 »


Recent Comments
  • Hakbor: This works fine. But to get the NUnit to use my current tests (and not the old ones) , it is not sufficient...
  • Alberto: Your plugin is very useful; I installed it on several different blogs I manage and I’m very happy with...
  • Nelson: Saved me from doing it myself. Good article.
  • andy: i am currently playing taiwanese server wow in 奈辛瓦里(PVP) and i would like to realm transfer to...
  • berties: any english speaking playing on a taiwanese server?