String-less ArgumentNullException

Sunday, December 9, 2007

The post I wrote yesterday about Expression Trees, and Jafar Husain's work, have inspired me to find some more cool usages for this feature. Consider this code: public class PersonRepository { public void Add(Person person, Context context) { if (person == null) throw new ArgumentNullException("person"); ...
tags: ,
3 comments

C# 3.0 New Feature for the Week #5: Lambdas and Expression Trees

Saturday, December 8, 2007

This week I'll talk about what seems to me like the coolest features of C# 3.0: lambda expressions and expression trees. Lambda Expressions We'll start with the part that is easier to understand. Remember anonymous methods from C# 2.0? Useful little bastards they are, only sometimes not so pleasant on the eyes. public void SortListIgnoreCase() { List<string> list = new List<string>{"abc", "ADE", "dol"}; list.Sort(delegate(string s1, string s2) ...
tags: ,
no comments

C# 3.0 New Feature for the Week #4: Extension Methods (and a PowerCollections Bonus!)

Saturday, December 1, 2007

Consider the following. You need to sort an IEnumerable<T>. By default, Sort is only available for T (arrays) and List<T> (generic list). So you write something like this (probably not the most efficient implementation, this is just an example): namespace Utils { public static class CollectionUtils { public static IEnumerable<T> Sort<T>(IEnumerable<T> collection) { List<T> toSort = new List<T>(collection); ...
no comments

C# 3.0 New Feature of the Week #3: Anonymous Types

Friday, November 23, 2007

This week's C# 3.0 New Features article will discuss a neat feature called anonymous types. You'll use it like this: var me = new {Name = "Doron", Age = 24 }; The result is an anonymous type created behind the scenes, with two properties: Name and Age. We assign it to a local variable with the "var" keyword, since we can't really mention the type (because of all the anonymity). To know more about the var keyword, read my first article about C# 3.0. Since it is a full blown type, and not some kind of dictionary, you will have...
tags: ,
5 comments

C# 3.0 New Feature for the Week #2: Object and Collection Initializers

Friday, November 16, 2007

Last week I started my series of posts about new C# 3.0 features, and my second post will be about object and collection initializers. I will also include a bonus - an easy way of having a collection initializer in C# 2.0. Object Initializers Say we have declared the following class (with the automatic properties syntax we learned about last week): public class Person { public string FirstName {get; set;} public string LastName {get; set; } } We...
tags: ,
no comments

C# 3.0 New Feature for the Week #1: Automatic Properties

Saturday, November 10, 2007

I've decided that I really need to get in the program and start learning C# 3.0 and Linq seriously. I've been planning to this for a long time, and even started playing with it a little, but never dug in really deep. So my new "C# 3.0 New Feature for the Week" series of posts is about getting myself to commit to studying and blogging about it. Every week I will present one new feature and talk about what is it good for and how it is implemented. So here we go. The first feature we'll talk about is automatic...
tags: ,
2 comments

WPF Binding, INotifyPropertyChanged and Linq

Friday, August 31, 2007

This is the first of a couple of tips I would like to share reguarding WPF. WPF binding is extremely powerful, but you are bound to run into a few issues, especially if, like myself, you have no WinForms experience. As I was writing my small LiveSpaceToBlogML GUI, I used binding in order to populate an object called ConversionOptions, which pretty much held all the data on the form. The form look something like this (a pretty simplified version, in order to focus on what matters): <Window x:Class="LiveSpaceToBlogML.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ...
9 comments

Anonymous Delegates or DisposableActions?

Friday, May 11, 2007

One of the most useful features of .NET 2.0 is anonymous delegates. They allow you to create "wrappers" for code which run before and after the code, handle exceptions in it, and decide whether to run it or not. Consider the following method: public static void WrapCall(Callback callback) { try { callback(); ...
tags: ,
no comments

My First C# 3.0 Program

Friday, March 23, 2007

I've finally decided to download the new "Orcas" CTP of Visual Studio, which includes support for the all new C# 3.0. Up until now I only read about it, so I thought it was about time I tried it myself. And it's awesome. So, here's my "hello world" program. public void MyFirstProgram(Func<string,string> doSomethingWithString) { var result = doSomethingWithString("dlroW olleH"); Console.WriteLine(result); ...
no comments

If we didn’t have loops

Tuesday, February 27, 2007

I've stumbled upon this article which explains how to implement delegates in Java. The issue of implementing things we have taken for granted  reminded me of a course I took a few years ago, in a language called Scheme, which is a variant of Lisp. Scheme is a functional language, and has a very recursive nature. It has no loops. As exercises at this course, we had to implement things such as While and For loops and add them to the Scheme interpreter. This made me think of how I would implement loops if didn't have them at C#. I...
tags: ,
no comments