Tips

Resharper – Using custom patterns to catch subtle bugs with OfType<T>

One of the pitfalls with LINQ which I have fallen into on multiple occasions is around the use of the OfType<T> extension method with dictionaries. Either through carelessness or changes to the underlying variable (a class property being changed from list to dictionary for instance) I have ended up using OfType<…> on a dictionary which …

Resharper – Using custom patterns to catch subtle bugs with OfType<T> Read More »

F# equivalent of C#’s Object Initialization syntax

In C#, you can use the object/collection initialization syntax like this: The F# equivalent of object initialization is done like this: As for collection initialization, you have a far more diverse range of tools available to you, for example: You can also create slices of an existing array: You can even add your own multi-dimensional …

F# equivalent of C#’s Object Initialization syntax Read More »

F# – Converting to and from Units of Measure

If you’re reading this post, you probably know about F#’s Units of Measure already, it’s very useful when working with real-world units and adds extra safety to code that needs to work with and convert from one unit to another. Here’s a quick snippet that shows you how to define and use units-of-measure: This code …

F# – Converting to and from Units of Measure Read More »

F# – Define empty class, struct or interface types

In C#, you define an empty class, struct, or interface like this: So how do you define an empty type in F#? Well, whenever you define a new class in F#, the compiler infers the class and end tokens at the beginning and end of the class’s definition, as you can see from below: So …

F# – Define empty class, struct or interface types Read More »

.Net Tips – use [field:NonSerialized] to stop serializing your event handlers

In C#, when you define an event in your class, e.g.: the event handlers will be serialized along with other properties, etc. This is because under the hood, the compiler translates your event into the following, as can be seen through JetBrain’s dotPeek decompiler: Since the generated EventHandler is not marked with the [NonSerialized] attribute …

.Net Tips – use [field:NonSerialized] to stop serializing your event handlers Read More »

F# – Adding custom indexer and slicer to your type

Indexer If your have a type that represents a collection of values, adding a custom indexer gives you a natural way to index directly into the object using the .[ ] operator. Take this simple Calendar class for instance, which keeps a map (F# equivalent of a Dictionary<TKey, TValue>) of notes against DateTime values: By …

F# – Adding custom indexer and slicer to your type Read More »

.Net Tips – Getting the default value of a type outside of generics

To get the default value of a type, you’ve probably used the default keyword in .Net already: 1: var defaultInt = default(int); // 0 2: var defaultObj = default(string); // null However, the use of the default keyword requires a type name to be specified at compile time, so you won’t be able to use …

.Net Tips – Getting the default value of a type outside of generics Read More »

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close