March 3rd, 2009

Sending an e-mail is pretty old news by now so it should come as no surprise that .NET contains a significant SMTP mail client. One old programming truth still holds: All programs will expand to eventually include sending (and receiving) e-mails. So how about adding e-mail to your program ? This post explores how we can use the System.Mail.SmtpClient to send formatted e-mails. There are plenty of options available and we will explore most of them.
Read the rest of this entry »
Tags: c#, email
Posted in Beginner, Learn C# | 7 Comments - getting there! »
March 2nd, 2009

You cannot post to a blog on a regular basis without obtaining somewhat of an (unhealthy) obsession with statistics. There are just too many fascinating tools available. I am a recovering addict which should give me more time to actually write articles and code. But some interesting observations can be made about Visual Basic versus C# and Java, the C# job market and where all these C# developers actually live.
Read the rest of this entry »
Tags: C# jobs, Java
Posted in Personal | 9 Comments - getting there! »
February 24th, 2009
Sometimes it is handy to have a quick overview of all the assemblies that your C# program has loaded. Maybe because you are trying to debug a version conflict, or because you want to distribute your application and want to get an idea of its dependencies.
Read the rest of this entry »
Tags: c#, reflection
Posted in Intermediate, Learn C# | 3 Comments - getting there! »
February 24th, 2009
Today I needed to automate posting some data to a web form from a C# program, and sure enough this is not at all that difficult. But surprisingly you need to call quite a large number of methods to get your data ready to ship. A working code example only took a few minutes to write but it took quite a bit more time to clean things up. You can find the full implementation at the end of this post.
Read the rest of this entry »
Tags: c#, networking
Posted in Beginner, Learn C# | 16 Comments - getting there! »
February 23rd, 2009
Your C# program has just calculated the weekly sales report and you need to upload it to the company file server. The C# System.Net.Webclient class makes this quite trivial. The same for downloading a file from the server and then parsing it for content. This post shows how you can use basic FTP actions to upload, download files and either store them in memory or write them to disk.
At the end of this post you will find my BasicFTPClient class that implements the uploading and downloading code.
Read the rest of this entry »
Tags: networking
Posted in Beginner, Learn C# | 5 Comments - getting there! »
February 17th, 2009

In this post I look at how we can simply retrieve basic information about our own types, discover their methods and lastly how we can import a foreign assembly, inspect its contents, load a class and execute its methods.
When C# compiles your code it not only stores the program in the assembly but the complete information on each of the structures you have defined. Each class, its types, parameters and methods described into detail are all available. This might sound like stating the obvious, but until recently most compilers treated this kind of “meta” data as a waste of space and never included it into the final assembly.
Read the rest of this entry »
Tags: c#, reflection
Posted in Chapter | 1 Comment already!»
February 16th, 2009

A union is a data structure that stores several different types of data at the same single location in memory. Although this might sound like madness in a garbage collected world many older binary data formats still heavily rely on this. A possible use is if you want to modify an IPv4 address. An IPv4 address is 4 bytes, or an unsigned C# int. You might want to manipulate the IP address as a single value, or by splitting it into four separate bytes.
Read the rest of this entry »
Tags: c#, language
Posted in Uncommon C# | 1 Comment already!»
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! »