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 9th, 2009
If you are up to date with your Ubuntu distribution you will be using Mono 1.9.1 which has support for C# LINQ build in. Enabling it is as easy as adding “using System.Linq” to your project. The following code snippet is the LINQ equivalent of a “Hello World” application:
using System;
using System.Linq;
class MainClass
{
public static void Main(string[] args)
{
String[] world = { "Hello World", "Hello Mars" , "Hello Venus" };
var rightOne = from s in world where s.EndsWith("World") select s;
foreach(string planet in rightOne)
Console.WriteLine("{0}",planet);
}
}
The first time I tried compiling it, MonoDevelop gave me the following error:
An implementation of `Where’ query expression pattern could not be found. Are you missing `System.Linq’ using directive or `System.Core.dll’ assembly reference?(CS1935)]
You can solve this by ensuring that you have added “System.Core” as a reference to your solution/project. You can do this by right clicking “References” in the “Solution Panel” and selecting “Edit References”.
Tags: language, LINQ
Posted in Uncategorized | 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# | 1 Comment already!»
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 8th, 2009

In early December 2008 Moonlight 1.0 beta was released by the Mono team, making it possible to run basic Silverlight 1.0 applications from your Linux Firefox installation. If you are like me mostly working on a Linux workstation, then its likely that you have seen little so far of the Microsoft Silverlight platform. The best way to think of Silverlight 1.0 is as Flash, but then by Microsoft. Similar to Flash, Silverlight installs as a web browser plugin and provides animation, graphics and audio/video. The next generation (Silverlight 2.0) includes support for running C# managed code from the plugin which should make things quite interesting.
Read the rest of this entry »
Tags: firefox, mono, moonlight, silverlight
Posted in Web | 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# | 1 Comment already!»
January 5th, 2009

In an earlier post I looked at how I could add my own icon to the Gnome Notification area from my Mono C# applications. The next thing I wanted to do was to show a notification in the taskbar to the user when something truly important happens. If you are running Ubuntu Intrepid (8.10) then its easy to create them from Mono. As I found out, Hoary (8.04) doesn’t include the required libraries.
Read the rest of this entry »
Posted in Beginner, GTK, Learn C# | 1 Comment already!»