This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Try to describe polymorphism as easy as you can
What is polymorphism?
Please read MSDN which covers it in reference to c#,
Basically a derived class inherits from another class it gets all its methods,events and properties, and every type is polymorphic in .NET since they all have Object as their base class.
Related
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.
This question already has answers here:
When should I use a struct rather than a class in C#?
(31 answers)
Closed 4 years ago.
Why POCO objects are created using classes instead of structs in C#, while POCO is intended to carry only public attributes and not any behaviors?
Do we really need to create a class with get and set accessors? I think structs would be much cleaner and simple to carry object's state.
In C#, struct type is a value, so looks that it is not a good choice.
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
This question already has answers here:
How do arrays in C# partially implement IList<T>?
(6 answers)
Closed 8 years ago.
I have a class constructor that takes a IList<IElement> as an argument.
When creating a new instance of the class I'm able to pass a IElement[] instead of the IList<IElement> how is that posible?
An array with element type T derives from IList<T>.
This is not visible in the meta-data in mscorlib.dll, but the inheritance relationship is created at runtime in the CLR. C# and the CLR are aware of the array type and treat it specially.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Enum “Inheritance”
I've been trying to do something like inheritance in Enum. I wanted a base Enum with multiple values from different Enums.
the best approach will be answered below.
Enum is a value type and consequently sealed, i.e. cannot be inherited.
See also Enum “Inheritance”