How to check if a interface reference is a specific class [duplicate] - c#

This question already has answers here:
Type Checking: typeof, GetType, or is?
(15 answers)
Checking if the object is of same type
(4 answers)
How can I check if an object is of a certain type at runtime in C#?
(9 answers)
Closed 3 years ago.
I was wondering if there is a way to find if my interface reference is a specific class.
For example i have DeviceInterface reference, and Playstation, PC and Mac all implement it. Is there a way to see if DeviceInterface is a PC?
I have thought about using a enum to define the type and using that, but is there a way of avoiding this and using a type check or something along those lines?
Thanks in advance.

Let's say that you have
DeviceInterface PcDevice = new PC();
In that case you can just do:
if (PcDevice is PC) { console.WriteLine("I'm a PC"); }
read more here to understand the is and as operators better

Related

Passing <T> from a string parameter to use AddSingleton [duplicate]

This question already has answers here:
How do I use reflection to call a generic method?
(8 answers)
Closed 2 years ago.
Why can't I do this?
services.AddSingleton<Type.GetType("ShoppingCartCache",true)>();
OR is there a better way of passing from a string
The method also has an overload to pass a type as parameter. So you can do:
services.AddSingleton(typeof(ShoppingCartCache));
Same also works with your example (although more prone to runtime errors):
services.AddSingleton(Type.GetType("ShoppingCartCache",true));
The reason it doesn't work is because generic types must be static, thus known at compilation.

Casting Abstract Type Object to Derived Type [duplicate]

This question already has answers here:
Is it possible to get c# to use method overload of most specific type rather than base type?
(4 answers)
up-casting in C# and call a specific method based on the derived type
(8 answers)
Closed 3 years ago.
I have a third party API that passes a parameter that is a base type. I handle each derived type separately as they have specialized functionality for each that I need to make use of.
if(parameter is DerivedTypeX) HandleParamerter(parameter as DerivedTypeX);
else if (parameter is DerivedTypeY) HandleParameter(parameter as DerivedTypeY);
...
For some reason, I can't let it go that there must be a more elegant way to handle this parameter than a massive else if block.

What is the negation of the test "is"? [duplicate]

This question already has answers here:
C# : 'is' keyword and checking for Not
(13 answers)
Closed 4 years ago.
To test if an object is of a certain type then we write if ( myObject is SomeClassName )
But how do we write if I want to test that the object is not of the mentionned type ?
if (!(myObject is SomeClassName))
Is probably the cleanest way of checking not is. Just checks if it is the type of object then flips it.
There isn't one, you have to wrap it in a ! operator:
if (!(myObject is SomeClassName))

Getting all interfaces of a C# class in Visual Studio [duplicate]

This question already has answers here:
How to find out which interfaces a .net class implements?
(4 answers)
Closed 4 years ago.
Is there a way to get a list of all interfaces of a C# class in the Visual Studio UI without digging through the superclass chain step by step? If not even a list on MSDN would be useful.
For example I can't see that Form is IDisposable without digging down to Control.
GetInterfaces:
typeof(List<string>).GetInterfaces()
returns
Type[] (8 items)4
typeof(IList<String>)
typeof(ICollection<String>)
typeof(IEnumerable<String>)
typeof(IEnumerable)
typeof(IList)
typeof(ICollection)
typeof(IReadOnlyList<String>)
typeof(IReadOnlyCollection<String>)
If you need to know a particular interface, you can use IsAssignableFrom:
typeof(ICollection).IsAssignableFrom(typeof(List<string>))
returns true

Is that correct using of "this" keyword? [duplicate]

This question already has answers here:
When do you use the "this" keyword? [closed]
(31 answers)
In C#, is "this" keyword required? [duplicate]
(6 answers)
Closed 9 years ago.
I would like to know if I get it correctly: also with this keyword I can distinct between fields and variables?
Like this:
class X
{
int x;
public X(int x)
{
this.x=x;
}
}
Yes, if a method parameter (or local variable) has the same name as a field, you need to use this to distinguish the two. Also, StyleCop is very vocal about every class member access being done through this, but whether that's a good idea or not may be up to debate. It makes things more clear, but also adds much visual clutter.

Categories