© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
7 replies
stigzler

Casting types inline

I'm still not clear on all the routes to this, and which one is best for readability. I'm trying to get
.Count
.Count
from a child object of
object sender
object sender
. The Type hierarchy is:

DirectDragTreeViewItem.GistViewModel.GistModel.IList<GistFileModel>
DirectDragTreeViewItem.GistViewModel.GistModel.IList<GistFileModel>


Here are a couple of ways I've tried:

int directCastListCount = (GistViewModel)((DirectDragTreeViewItem)sender).DataContext) // wtf!? gave up here
int paternMatchedListCount = ((((sender as DirectDragTreeViewItem).DataContext as GistViewModel).Gist as GistModel).Files as IList<GistFileModel>).Count();
int directCastListCount = (GistViewModel)((DirectDragTreeViewItem)sender).DataContext) // wtf!? gave up here
int paternMatchedListCount = ((((sender as DirectDragTreeViewItem).DataContext as GistViewModel).Gist as GistModel).Files as IList<GistFileModel>).Count();

Of course things could be nested deeper. Which is the best way (realise 'best' is ambiguous)
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

Multiple types in a params without casting?
C#CC# / help
4y ago
Interaction of 'first class spans' with inline array types/Extension methods over inline array types
C#CC# / help
6mo ago
✅ Type Casting
C#CC# / help
4y ago