March 17th, 2009

One of the best things about C# is that as the language and libraries expand thought is put into keeping things readable. Below I have listed 10 shorthands that you can use to make your code tighter and less wordy. No doubt you know one or more already — but do you currently use all ten of them ?
Read the rest of this entry »
Tags: c#
Posted in Learn C# | 21 Comments - getting there! »
March 4th, 2009

One of my favorites in the PHP libraries is the strip_tags function. Not only does it neatly remove HTML from an input it also allows you to specify which tags should stay. This is great if you are allowing your visitors to apply some basic HTML tags to their comments. This post explores two issues: using C# to remove unwanted tags, and cleaning up unwanted attributes that might be hidden in the allowed tags.
Read the rest of this entry »
Tags: c#, html, striptags, strip_tags
Posted in Beginner, Learn C# | 8 Comments - getting there! »
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# | 5 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# | 8 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# | 2 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# | 2 Comments - getting there! »