I want to implement the typestate pattern in C#. I’ve tried something like:
interface Open {}
interface Closed {}
abstract class Foo<T> { }
class Bar<T> where T: Closed { }
class Bar<T> where T: Open { }
as well as other permutations, but they all end up with the compiler complaining.
I don’t know enough about C#’s type system to know what will actually work, and I can’t find much documentation about how to do this. Is it possible to implement the type state pattern? I found Adam’s blog however he doesn’t actually implement the type state pattern as shown in 1, which is what I’d like to do if possible.
Ideally I’d like something like
struct Closed { };
struct Open { };
class Foo<T> {
// functions applicable to Foo's in both the Closed and Open states
}
class Foo<Closed> {
// functions applicable to Foo's in just the Open state
}
class Foo<Open> {
// functions applicable to Foo's in just the Open state
}