Your code does not follow the pattern matching syntax; I don't see "is" anywhere. That's what is actually doing the casting
.NET
Getting started
Useful resources
IDEs and code editors
- Visual Studio (Windows/Mac)
- Rider (Windows/Mac/Linux)
- Visual Studio Code (Windows/Mac/Linux)
Tools
Rules
- Rule 1: Follow Lemmy rules
- Rule 2: Be excellent to each other, no hostility towards users for any reason
- Rule 3: No spam of tools/companies/advertisements
Related communities
Wikipedia pages
- .NET (open source & cross platform)
- .NET Framework (proprietary & Windows-only)
I see.
The book uses a very specific scenario where o
is an object that would accept any type. So using the object data type worked. Check the OP for the edit.
I see, using "is" could be a downcast from any type. But from object it would always be an upcast so you don't need an explicit casting operator
An integer will never be a string. Originally you create an integer variable so it's telling you the string case is pointless.
So c# in runtime already knows the type of o(unless you do some silly magic ofcourse) If you wanna change o for debug purposes you can try .GetType() and typeOf
You can check the type with
bool isString = o.GetType() == typeof(string);
(Sorry for any errors I'm on phone so code fiddlers aren't that great)
The book uses a very specific scenario where o
is an object that would accept any type. So using the object data type worked. Check the OP for the edit.
The "is" is a part of pattern matching which I don't believe regular switches can do. Only switch expressions. The example in the link you gave is checking the type by casting using "is".