Why is it saying "Cast is redundant"? If I remove the cast it instead says How can I target and use the is statement "toy is Featuer feather" otherwise?
Working example: Only says "Cast is redundant" by doesn't through any errors.
- if (input == 1 && (object)toy is Feather feather)
- {
- Console.WriteLine("ba");
- typeOfToy = feather;
- }
Working example: Foreach lets me target the "toy is Feather feather" without any errors. But putting every is statement in a foreach seems overkill?
- foreach (var toy in this.toy)
- {
- if (input == 1 && toy is Feather feather)
- {
- Console.WriteLine("ba");
- typeOfToy = feather;
- }
- }
Not working example: Casting (object) has been removed and through: "An expression of type List cannot be handled by a pattern of type 'Feather'. Why is that and what's the best practise using my two examples above? Any other ways to solve this?
- if (input == 1 && toy is Feather feather)
- {
- Console.WriteLine("ba");
- typeOfToy = feather;
- }