Functional C#: Providing an Option

Sorry for the terrible pun in the title.  I wanted to blog about developing an F# style Option class for C# and I couldn’t resist. The basics of an Option class are very straight forward.  It’s a class that either has a value or doesn’t.  It’s almost like nullable but for every type and allows…

5

RantPack Update 2.0.0.2

Version 2.0.0.2 released.  Summary: RantPack is a utility library I maintain and actively use.  The main themes of this library are functional programming, patterns, immutable/pressitent collections, future and other threading primitives.  I’ve placed the code and binaries on http://code.msdn.microsoft.com/RantPack for educational and sharing purposes. Release Link: https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=RantPack&ReleaseId=1306 Release Notes: Renamed Tuple properties to be more…

1

Yet another rule for Equality

“If you implement equality in a child class, including operators, you must implement the equality operators in the base class.” Unfortunately this is another case of learn the hard way but makes sense when you think about it.  The below code snippet is an example of the problem that I hit.  Even though I have…

6

Enums vs. Adapters

I like Enums and use them frequently for options and behavior.  To an extent I use Enum’s to control behavior.  For example enum Kind { Kind1, Kind2, Kind3 } class Example { private Kind m_kind; public int SomeAction() { switch (m_kind1) { case Kind.Kind1: return ActionForKind1(); case Kind.Kind2: return ActionForKind2(); case Kind.Kind3: return ActionForKind3(); default:…

9

Making Equality easier

Recently I’ve done a bit of posting about the difficulties of properly implementing equality in VB (and DotNet in general).  While most of the problems can be fixed with a standard snippet the one really hard to implement issue is GetHashCode().  The rules for GetHashCode() are both simple and seemingly contradictory If two objects are…

2

RantPack Update

I released a new version of RantPack today.  Mostly this is a bug fix release with a couple of minor new features. https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=RantPack&ReleaseId=1119 Features Added a way to shim Immutable collections to non-immutable interfaces to increase the level of interoperability.  The collections are still immutable and throw an exception whenever one of the mutable APIs…

0

do {} while(0) what?

A recent check in of mine raised a few eye brows during reviews.  I checked in a few macros which ended with/contained a “do{}while(0)” and people were curious as to why.  In my experience there are two main uses for it.  Insert an empty statement with no runtime penalty Group a set of statements into…

3

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

IEquatable(Of T) and GetHashCode()

This is a bit of a follow up to a previous post we discussed how to properly implement equality in VB.  Several users commented/asked that IEquatable(Of T) could be used in place of overriding Equals().  Since IEquatable(Of T)  doesn’t define a GetHashCode() method the user didn’t need to define it and hence run into all…

0

Properly Implementing Equality in VB

Many developers want to implement equality functions for their objects.  DotNet made equality a deep part of the framework and added support all the way up to System.Object with Equals and GetHashCode.   In addition to the strongly enforced method semantics of GetHashCode and Equals there are also several other hard to enforce patterns that developers…

6