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

One of the pit­falls with LINQ which I have fall­en into on mul­ti­ple occa­sions is around the use of the OfType<T> exten­sion method with dic­tio­nar­ies. Either through care­less­ness or changes to the under­ly­ing vari­able (a class prop­er­ty being changed from list to dic­tio­nary for instance) I have end­ed up using OfType<…> on a dic­tio­nary which …

Resharp­er – Using cus­tom pat­terns to catch sub­tle bugs with OfType<T>Read More »

F# equivalent of C#’s Object Initialization syntax

In C#, you can use the object/collection ini­tial­iza­tion syn­tax like this: The F# equiv­a­lent of object ini­tial­iza­tion is done like this: As for col­lec­tion ini­tial­iza­tion, you have a far more diverse range of tools avail­able to you, for exam­ple: You can also cre­ate slices of an exist­ing array: You can even add your own mul­ti-dimen­­sion­al …

F# equiv­a­lent of C#’s Object Ini­tial­iza­tion syn­taxRead More »

F# – Converting to and from Units of Measure

If you’re read­ing this post, you prob­a­bly know about F#’s Units of Mea­sure already, it’s very use­ful when work­ing with real-world units and adds extra safe­ty to code that needs to work with and con­vert from one unit to anoth­er. Here’s a quick snip­pet that shows you how to define and use units-of-mea­­sure: This code …

F# – Con­vert­ing to and from Units of Mea­sureRead More »

F# – Define empty class, struct or interface types

In C#, you define an emp­ty class, struct, or inter­face like this: So how do you define an emp­ty type in F#? Well, when­ev­er you define a new class in F#, the com­pil­er infers the class and end tokens at the begin­ning and end of the class’s def­i­n­i­tion, as you can see from below: So …

F# – Define emp­ty class, struct or inter­face typesRead 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 han­dlers will be seri­al­ized along with oth­er prop­er­ties, etc. This is because under the hood, the com­pil­er trans­lates your event into the fol­low­ing, as can be seen through Jet­Brain’s dot­Peek decom­pil­er: Since the gen­er­at­ed Even­tHandler is not marked with the [Non­Se­ri­al­ized] attribute …

.Net Tips — use [field:NonSerialized] to stop seri­al­iz­ing your event han­dlersRead More »

F# – Adding custom indexer and slicer to your type

Index­er If your have a type that rep­re­sents a col­lec­tion of val­ues, adding a cus­tom index­er gives you a nat­ur­al way to index direct­ly into the object using the .[ ] oper­a­tor. Take this sim­ple Cal­en­dar class for instance, which keeps a map (F# equiv­a­lent of a Dictionary<TKey, TVal­ue>) of notes against Date­Time val­ues: By …

F# – Adding cus­tom index­er and slicer to your typeRead More »

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

To get the default val­ue of a type, you’ve prob­a­bly used the default key­word in .Net already: 1: var default­Int = default(int); // 0 2: var default­O­bj = default(string); // null How­ev­er, the use of the default key­word requires a type name to be spec­i­fied at com­pile time, so you won’t be able to use …

.Net Tips – Get­ting the default val­ue of a type out­side of gener­icsRead 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.