© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
23 replies
LukeJ

✅ Invalid cast between interface types

Hello. I'm trying to learn functional programming in C#, so I'm ending up with some unusual code. A part of that is having an array of options marked with their length, like so
IMarked<Arr<Option<Unit>>, CountOfField>
IMarked<Arr<Option<Unit>>, CountOfField>

Since this is so long, I've defined this
public interface FieldUnits : IMarked<Arr<Option<Unit>>, CountOfField> {}
public interface FieldUnits : IMarked<Arr<Option<Unit>>, CountOfField> {}

This inevitably means casting to FieldUnits at some point, and doing so is giving me this error
System.InvalidCastException: 'Unable to cast object of type 'Marked`2[LanguageExt.Arr`1[LanguageExt.Option`1[Unit]],CountOfField]' to type 'FieldUnits'.'
System.InvalidCastException: 'Unable to cast object of type 'Marked`2[LanguageExt.Arr`1[LanguageExt.Option`1[Unit]],CountOfField]' to type 'FieldUnits'.'

I'm not sure why though, since they seem to be the same. I also swear this was working the other day. Any ideas what's wrong here? Here's a sample line that has this problem
var units = (FieldUnits)Enumerable.Repeat(Option<Unit>.None, CountOfField.Count).ToArr().mark(default(CountOfField));
var units = (FieldUnits)Enumerable.Repeat(Option<Unit>.None, CountOfField.Count).ToArr().mark(default(CountOfField));
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Interoperability between incompatible types
C#CC# / help
4y ago
✅ Conversions of types between two libraries
C#CC# / help
2y ago
❔ Cast is not valid when casting interface to comcrete impl
C#CC# / help
3y ago