Dictionary<TKey,TValue>.TryGetValue and Anonymous Types

One of the methods I find to be the most useful in .Net is the method Dictionary<TKey,TValue>.TryGetValue.  This method is a nice compromise between performance, explicit return vs. exception, and a being verbal about the chance of failure.  It returns false on failure and uses an out parameter to return the actual requested value.  This…

7

Making F# type inference friendly for C#

One of my current hobby projects, VsVim, requires me to make a lot of calls between F# and C# projects.  The core Vim engine is a pure F# solution based on Visual Studio’s new editor.  It additionally has a small hosting layer and a large test bed both written in C#.  When working with the…

5

Using F# Discriminated Unions in C# (Beta2)

While updating my VsVim editor extensions for Beta2 [1] I got hit by a change in the way F# exposed discriminated unions in metadata.  My extension consists of a core F# component with a corresponding set of unit tests written in C#.  It’s mostly API level testing and as such I use a lot of…

5

When to use Type Inference

Occasionally the debate will come as to when it’s OK to use type inference in order to declare a variable.  There appear to be three groups in this debate. Whenever it’s possible Only when it’s absolutely clear what the type is Never, type inference is evil I fall into camp #1 and here are my…

27

Switching on Types

One action I find frustrating in C# is where a particular action needs to be taken based off of the type of a particular object.  Ideally I would like to solve this with a switch statement but switch statements only support constant expressions in C# so no luck there.  Previously I’ve had to resort to…

9

Design Guidelines: Provide type inference friendly Create function for generic objects

Really this guideline is a bit longer but putting it all in a blog title seemed a bit too much.  The full guideline should read: "If a generic class constructor arguments contain types of all generic parameters, provide a static method named Create on a static class of the same class name as the generic…

3

Tuples Part 3: Type Inference Friendly Constructor

Last time we were left with a constructor that required us to explicitly specify generic parameters.  This is not always easy or possible.  We’ll now alter the script to generate a constructor which utilizes type inference to create a Tuple.  In addition, all tuples will use the same overloaded method making the creation uniform.  The…

1

C# Lambda Type Inference

One of the limitations of C# type inference is that you cannot use it to infer the type of a lambda expression.  For example, the following code will not compile var f = () => 4; Normally this is not too much of an issue because you can just explicitly declare the type of the…

1

Type Inference and IEnumerable

This is somewhat of a follow up on a previous post I did on the difference between IEnumerable(Of T) and the IEnumerable interfaces.  I’ve seen several people type in the following code and wonder if there was a fundamental bug in the type inference code. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)…

2