Skip navigation
top menu

c#

Vala - GNOME Live!

Vala is a new programming language that aims to bring modern programming language features to GNOME developers without imposing any additional runtime requirements and without using a different ABI compared to applications and libraries written in C.

http://live.gnome.org/Vala

Implicit conversions in C#

Yesterday I came across an interesting article: What's the opposite of Nullable. While the solution for Non-Nullability is interesting, the reason i'm blogging this is because the article also used a C# feature which i didn't know of: implicit conversions.

And guess what? Today I had a situation where I could use these implicit conversions. My app reads data from a CSV-file, so all the input are just strings. Until now that was just fine. However, at one part of my code I had to process one of the fields which has a fixed format. Say a field is a phonenumber and i need the country-prefix. So I created a PhoneNumber-class like this:

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
public class PhoneNumber
{
  private readonly string _number;
 
  public PhoneNumber(string number)  {
      if (!new Regex(PHONE_NUMBER_REGEX).IsMatch(number))
      {
          throw new ArgumentException("Invalid phone number", "number");
      }      _number = number;
  }
  
  public string Prefix
  {    get { return GetPrefixFromNumber(...); }
  }
}

Now it is possible to create a (helper-)method in another class to get the prefix like this (this is just a simple example):

1
2
3
4
public string GetPrefix(PhoneNumber phoneNumber)
{
  return phoneNumber.Prefix;
}

But when you only have the phonenumber as a string, you'll still have to create an instance of PhoneNumber to be able to call the GetPrefix method:

1
var prefix = GetPrefix(new PhoneNumber("+32485123456"));

That is, until we add a method for implicit conversion to the PhoneNumber-class:

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
public class PhoneNumber
{
  private string _number;
 
  public PhoneNumber(string number)  {
      if (!new Regex(PHONE_NUMBER_REGEX).IsMatch(number)
      {
          throw new ArgumentException("Invalid phone number", "number");
      }      _number = number;
  }
  
  public string Prefix
  {    get { return GetPrefixFromNumber(...); }
  }
  
  public static implicit operator PhoneNumber(string number)
  {    return new PhoneNumber(number);
  }
}

Now it is possible to call the GetPrefix method with just a string. The string will be automagically converted to a PhoneNumber, unless it is invalid, in which case the ArgumentException will be thrown:

1
var prefix = GetPrefix("+32485123456");

Mono 2.0 Packages for Ubuntu Intrepid | eric's extremeboredom

I was disappointed to read that Intrepid will not include Mono 2.0, so I created packages. They’re available for download from my PPA.

http://eric.extremeboredom.net/2008/10/15/296

Codekana: Advanced C/C++/C# Code Visualization for Visual Studio - Features

Codekana analyzes your C/C++/C# code while you edit it and uses the information it gathers to present you with a richer, more informative view of your source code.

http://www.codekana.com/

Visual Studio Gallery

Products & Extensions for Visual Studio

http://visualstudiogallery.com/

String.Format("{0}", "formatting string"};

Quick reference for the C# String.Format method

http://idunno.org/archive/2004/14/01/122.aspx

Ninject: Lightning-fast dependency injection for .NET

Ninject is an ultra-lightweight, ultra-fast dependency injection framework for .NET 2.0 applications. Easy to use, fast, flexible, and powerful, Ninject aims to make inversion of control accessible to all types of .NET projects, regardless of size or comp

http://ninject.org/

ASP.NET Validator Controls

This tutorial gives a brief overview of how to use the ASP.NET Input Validation Controls.

http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=46

.NET Cheat Sheets « Just Sayin’ More Words

Some useful .NET sheatsheets: .NET Format String Quick Reference, ASP.NET 2.0 Page Life Cycle & Common Events, ...

http://john-sheehan.com/blog/index.php/net-cheat-sheets/