I used a Visual Studio Code’s Polyglot notebook and wrote the following in 3 separate cells, in the order indicated below.
Cell 1:
class Z;
Cell 2:
class A
{
public static void f()
{
Console.WriteLine(Z.x);
}
}
Cell 3:
class Z: A
{
public static int x = 42;
}
I then executed the cells in order, and got the following error message.
'Z' does not contain a definition for 'x'
My original question was:
Is it possible, in C#, to write a (possibly abstract) class
A
that has a (possibly static) function with signaturevoid f(B b)
, whose parameter’s typeB
is a (possibly abstract) class that derives fromA
(class B: A {...}
)? If not, why not?
and it was titled Can a C# class use a derived class?
I accepted the answer below based on this formulation. This formulation was later deleted by another user who edited my question, and changed its title.
3
Answers
Yes, sure. This is fully valid:
Of course, the two classes need to be in the same assembly, because they need to know each other. The other question is for a relevant use case, which I find hard to imagine. Normally, if A is abstract, you would return A’s (which then need to be derived types of A), and not B’s, since that prevents you from later creating a class C that derives from A and which would replace B.
Yes, it is possible.
Here’s an example that follows exactly what you requested:
Output:
Live demo
Note:
Since you asked,
A
and/orB
can also be abstract.Update:
The questiom was editted after this answer was given, with additional details about the usage of Polyglot notebook.
Regarding this additional information: the problem is related to the way the notebook executes the cells one by one. It is not an issue related to C# in general or the usage of inheritance in particular.
The notebook executes the cells one by one. So when you execute cell 2,
Z
is nothing but aclass
, without any notion ofx
. That’s why you get the error.It’s not clear what behaviour you actually want. Of course you can just write the entire code into a single cell:
So your problem is not related to inheritance, but to how Polyglot executes the cells. Be aware, though, that your approach is pretty strange. A base-class should never make any assumptions about derived classes, this it it shouldn’t even know that there exist any derived classes in the first place. However that’s more of a conceptual problem, which is way too broad for this questions scope.