Here is a bit of a puzzle – what does the following program print? using System; class Parent { public void Foo(String str) { Console.WriteLine(“Parent.Foo”); } } class Child : Parent { public void Foo(Object str) { Console.WriteLine(“Child.Foo”); } } class Example { static void Main() { Child child = new Child(); child.Foo(“test”); } } [...]
Category Archives: Code
Overload resolution puzzler
07-Nov-10Lambda expressions are powerful tools that allow you to create succinct inline functions. However, without proper care they can quickly descend into an unreadable mess. While there are several ways that a lambda expression can become messy, this article will only address the naming conventions used for the expression’s parameters. Here are the two different [...]
Two structural additions to C#
28-Apr-10In What’s in a name? (C#’s hidden support for structural typing) we explored a few of the areas in which C# currently supports structural typing. I would like to propose two new additions to C#’s structural typing abilities: implicit interface inheritance and richer generic constraints. Implicit interface inheritance I propose that class or struct ought [...]
C# is well known for its nominative type system. This means that C# identifies types and their relationships based on their names. That is why C# does not allow you to create two types with the same name, even if they have a different public interface – the C# compiler only cares about the name [...]
Choosy developers choose interfaces
23-Apr-10Each time you write a parametrized method you must decide which are the best types to use for your parameters. If you are not careful, your choice could place unnecessary restrictions on the caller of the method that requires them to pass too specific of a type. When you are making this decision you should [...]
Most C# developers are very familiar with writing imperative code (even though they may not know it by that name). In this article, I will introduce you to an alternative style of programming called declarative programming. Proper declarative code is easier to read, understand, and maintain. As professionals, we should be striving to write better [...]
In part 1 of this series I explained the difference between reference equality and value equality. In this article I am going to demonstrate how to compare two reference types using value equality semantics. Override Object.Equals Every time you use the binary equality operator (==) or the Equals method on a reference type you are [...]
Make aliases an option
26-Nov-09Do you prefer int or Int32? How about string or String? Some developers have strong preferences, others don’t care. Personally, I prefer to use the actual CLR type names (in other words I prefer Int32 and String) and since I do, I feel as though I am a second-class citizen. Interestingly enough, it is Visual [...]
Smarter type inference with C# 4
03-Nov-09Here is a new addition to the C# language in version 4 that doesn’t seem to be getting much attention. The type inference algorithm has become smarter and for the first time we are able to infer generic type arguments from the return types of methods in certain cases. Consider this example that compiles in [...]
C# 3 gave us the lambda expression and ever since we have been using them to create abstract representations of terse, anonymous functions. While expressive and helpful, they are not always needed. Consider this example: using System; using System.Collections.Generic; class Example { static void Main() { new List<Int32> { 1, 2, 3 } .ForEach(i => [...]