Z
Zach
Hi everyone,
I'm a bit of a newbie to C# programming so forgive this innocent
question, but coming from a C++ background this seems very odd to me,
and I'm hoping someone can shed some light onto why things work this
way as opposed to the C++ way, which I see no real disadvantages in.
The issue arises when trying to implement an interface in C#. In C++
it is quite common to define an interface using a bunch fo pure virtual
functions and then implement it 2 or 3 levels up in the hierarchy. For
example,
class A
{
public:
virtual void f() = 0;
};
class B : public A
{
};
class C : public B
{
virtual void f() {cout << "f()" << endl;}
}
In C#, it seems this is not allowed by design (unless I'm missing
something obvious, which it certainly wouldn't be the first time). My
question is why? It seems to force you to implement all methods at the
very next level of inheritance. In the above example, C# compiler
would have forced me to implement the method f() in class B.
This method seems very inconvenient, however. For example, there are
plenty of pre-existing interfaces in the .NET base classes that provide
various services. What if you want a particular set of classes to all
provide this service? Normally, in C++, I would have made a base class
called like BaseServiceClass, which implemented this interface. Then
in the derived classes I would provide the implementation, and
elsewhere in my code I would store pointers to BaseServiceClass
objects, all of which could be used to call the method in question.
What is the paradigm for solving this kind of design problem in the C#
/ .NET world?
Thanks
I'm a bit of a newbie to C# programming so forgive this innocent
question, but coming from a C++ background this seems very odd to me,
and I'm hoping someone can shed some light onto why things work this
way as opposed to the C++ way, which I see no real disadvantages in.
The issue arises when trying to implement an interface in C#. In C++
it is quite common to define an interface using a bunch fo pure virtual
functions and then implement it 2 or 3 levels up in the hierarchy. For
example,
class A
{
public:
virtual void f() = 0;
};
class B : public A
{
};
class C : public B
{
virtual void f() {cout << "f()" << endl;}
}
In C#, it seems this is not allowed by design (unless I'm missing
something obvious, which it certainly wouldn't be the first time). My
question is why? It seems to force you to implement all methods at the
very next level of inheritance. In the above example, C# compiler
would have forced me to implement the method f() in class B.
This method seems very inconvenient, however. For example, there are
plenty of pre-existing interfaces in the .NET base classes that provide
various services. What if you want a particular set of classes to all
provide this service? Normally, in C++, I would have made a base class
called like BaseServiceClass, which implemented this interface. Then
in the derived classes I would provide the implementation, and
elsewhere in my code I would store pointers to BaseServiceClass
objects, all of which could be used to call the method in question.
What is the paradigm for solving this kind of design problem in the C#
/ .NET world?
Thanks