This question already has answers here:
Redundant to inherit from Object in C#?
(7 answers)
Closed 7 years ago.
In this MSDN example, the class explicitly inherits from Object:
class Point: Object {
// ...
}
Is explicitly inheriting from Object ever necessary? That is, is it not equivalent to the following?
class Point: Object {
// ...
}
No, it is implicitly inherited. This is why every class in C# has a .ToString() and .Equals().
https://msdn.microsoft.com/en-us/library/vstudio/system.object%28v=vs.100%29.aspx
Object class: "This is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy."
This type of inheritance where everything is derived from a single class is called a "unified type system"
Related
This question already has answers here:
Cast List<T> to List<Interface>
(10 answers)
Closed 3 years ago.
I have a class which implements an interface. A second class implements an IList<> of this first class. I need to assign this second class to a generic property which is an IList<> of the interface.
This is a demo for the code I use:
public class SODemo
{
public SODemo()
{
ClassWithIListOfClassWithInterface classWithIList = new ClassWithIListOfClassWithInterface();
IList<IDemoInterface> listOfInterfaces;
// CS0266: Cannot implicitly convert type ...
listOfInterfaces = classWithIList;
}
}
public class ClassWithInterface : IDemoInterface
{
// ...
}
public class ClassWithIListOfClassWithInterface : IList<ClassWithInterface>
{
// ...
}
From answers on similar problems I found out that it seems not work at all.
Why do I need this?
I have a lot of classes which are implemented like ClassWithIListOfClassWithInterface and I need a generic handler for them.
Question:
My goal is to access each element in listOfInterfaces through the methods implemented in the interface.
Is there any alternative I can use?
Edit
I already tried this
listOfInterfaces = (IList<IDemoInterface>)classWithIList;
but then I get an System.InvalidCastException at runtime.
The problem is not with your class itself, but rather with the conversion between IList<IDemoInterface> and IList<ClassWithInterface>.
IList<T> in C# are said to be "invariant in T". This means that you cannot make conversions between IList<T1> and IList<T2> directly, even if they have an inheritance relationship.
What you can do is to create an IList<IDemoInterface> and copy every element from your source IList<ClassWithInterface> to it. Of course that'd be rather cumbersome, so the LINQ way of doing it is that:
listOfInterfaces = classWithIList.Cast<IDemoInterface>().ToList();
This question already has answers here:
C# virtual keyword
(9 answers)
C# - Keyword usage virtual+override vs. new
(11 answers)
Can you write virtual functions / methods in Java?
(6 answers)
Closed 4 years ago.
In an interview it was asked why do we need to override method of base class.
I tried to answer like when we want to have different implementation in derived class.
But then he said, "Why don't we just create a new method with different name and different implementation instead of overriding base class method?", anyway we are not reusing base class method as implementation will be different then just create a new method instead of overriding.
I got confused what to answer. Could somebody explain.
public class BaseClass
{
virtual void Foo(){}
}
public class DerivedClass: BaseClass
{
override void Foo(){}
}
Generally we implement overriding like above.
What he said is like why do we need concept of overriding we can do like below
public class BaseClass
{
void Foo(){}
}
public class DerivedClass: BaseClass
{
void Foo1(){}
}
His question was looking weird I tried to explain but like its a method of base class we are just redefining it in derived class. In this way our code will be clean as well. But looks like he was not satisfied.
I would check this answer:
Why does this polymorphic C# code print what it does?
then try to grasp the concept of methods in an object basically having pointers to code. When you override implementation in an subclass then that becomes the new code pointed to, whether it's used or cast as a superclass or not.
So the main purpose of overriding is to create classes that inherit from one class but each have their own implementation and then be able to treat or operate on them equally the same as the original superclass. This is the essence of the Liskov Principle or the 'L' in SOLID. What is an example of the Liskov Substitution Principle?
This question already has answers here:
Get derived class type from a base's class static method
(8 answers)
Closed 9 years ago.
I'm trying to get the derived class type from a static method defined in the base class.
The structure looks like:
class BaseClass
{
public static Type GetType()
{
return MethodBase.GetCurrentMethod().GetType();
}
}
class Foo : BaseClass
{
}
I need the code Foo.GetType() to return Foo but it returns BaseClass :(
I have to get type without using generics or initializing an instance.
How can I achieve this?
Why not use typeof(Foo)? Without more context on what you're doing, it looks like that should work perfectly.
Since Foo doesn't redeclare GetType, Foo.GetType() is effectively the same method as BaseClass.GetType(). So when you write Foo.GetType(), the compiler emits a call to BaseClass.GetType(), since BaseClass is the type that actually implements the method.
Anyway, what you're doing doesn't make sense ; if you write Foo.GetType(), you already know that you want it to return Foo, so you can just use typeof(Foo).
This question already has answers here:
How do I check if a type is a subtype OR the type of an object?
(5 answers)
Closed 9 years ago.
Suppose I have a class that looks like this:
class Derived : // some inheritance stuff here
{
}
I want to check something like this in my code:
Derived is SomeType;
But looks like is operator need Derived to be variable of type Dervied, not Derived itself.
I don't want to create an object of type Derived.
How can I make sure Derived inherits SomeType without instantiating it?
P.S. If it helps, I want something like what where keyword does with generics.
EDIT:
Similar to this answer, but it's checking an object. I want to check the class itself.
To check for assignability, you can use the Type.IsAssignableFrom method:
typeof(SomeType).IsAssignableFrom(typeof(Derived))
This will work as you expect for type-equality, inheritance-relationships and interface-implementations but not when you are looking for 'assignability' across explicit / implicit conversion operators.
To check for strict inheritance, you can use Type.IsSubclassOf:
typeof(Derived).IsSubclassOf(typeof(SomeType))
Try this
typeof(IFoo).IsAssignableFrom(typeof(BarClass));
This will tell you whether BarClass(Derived) implements IFoo(SomeType) or not
This question already has answers here:
Create an instance of a class from a string
(8 answers)
Closed 9 years ago.
I know the type of object (let's say IAnimal) I need to instantiate, and the name (lets say Tiger). How do I write the code to instantiate Tiger, given that the variable that knows the object name is a string. I'm likely missing something simple here, but am currently stuck on this.
Update: I meant Class Tiger : IAnimal, changed above to reflect that.
Using the Activator.CreateInstance method
example:
// the string name must be fully qualified for GetType to work
string objName = "TestCreateInstance.MyObject";
IProcess txObject = (IProcess)Activator.CreateInstance(Type.GetType(objName));
or
object o = Activator.CreateInstance("Assem1.dll", "Friendly.Greeting");
See also: Reflection Examples C#
Use reflection to instantiate an object of a class by its type name.
object o = Activator.CreateInstance(Type.GetType("Tiger"));
Note that you cannot instantiate interfaces (judging by your naming), as they are merely a contract that defines what a specific class should implement.
I am not sure I fully understand the question. However, assuming that ITiger is actually a concrete class and not an interface (as the I* would suggest).
You can use reflection to create and instance of a type from a string.
Something like:
ITiger myTiger = Activator.CreateInstance("ITiger") as ITiger;
http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx
Is that what you are asking?