So I am trying to populate a series of lists that are properties of a class I have and wanted to build a generic method in another class to populate them but can’t figure out how to do it.

So the first class would look like this:
I am trying to then do this in the other class:
but it tries to cast it as one of the override types. Do I need to build an interface first that ListClass would extend first? or how would I do this?
Ok so the easiest way to do this is a method called Add that takes an object on the List class, this method will check the type of the object and add it to the right list, you can use if else or a switch statement, the method would look like this
Simple, the is pattern will take care of the casting for you and will also handle nulls so that’s pretty neat, this also eliminates the need for multiple overrides, the type checking is extremely fast so need to worry about the speed here, if you have any further questions let me know.
Btw those are no properties those are fields, properties have getters and setters, and should start with an uppercase letter, but no worries I took care of that for you on the snipped
If each list is a different type you can use pattern matching to determine which list to put each item in. For instance:
Or you can use LINQ to filter by each type in turn:
Fixed formatting.
Hello, ekolis: code blocks using triple backticks (“`) don’t work on all versions of Reddit!
Some users see this / this instead.
To fix this, indent every line with 4 spaces instead.
FAQ
You can opt out by replying with backtickopt6 to this comment.
C# devs
null reference exceptions

source