Why is this illegal in C#?
class Foo: Foo.BaseFoo //Circular base class dependency compile time error
{
private class BaseFoo {...}
...
}
I'm not arguing when this could be useful or not, but I'd like to know what are the reasons that would disallow such code to compile. A similar restriction happens with private interfaces.
UPDATE
Seeing that its a duplicate I'll center the question more in why this isn't valid with interfaces which seems more useful?
And, what's more, why does it seem to be legal with the Roslyn preview as shown here
This is not a problem of the access modifier -that the class is private. This is a circular base class dependency. You try to define a class called Foo that inherits a nested class called BaseFoo.
Related
Let's assume we have these two definitions
// assembly A
public abstract class A
{
internal abstract void func();
}
// assembly B
public class B : A
{
// internal override void func()
}
The error message says "B does not implement abstract member A.func()". I think that's bad design decision for reasons which I will try to describe in this post. I would be thankful for any insights for why the current design was chosen by creators of C# language. If I made internal members into Roslyn then I would surely create at least error saying something like "B cannot inherit from A which contains internal member" or ideally new keyword closed which forbids inheriting from another assembly without need of internal abstract member. Why? For these reasons:
a) Abstract class A may contain many members. Then I decide to inherit from class A in another assembly and after few days of work I implement most of its methods fixing one error by one. Then I suddenly see last error message saying that internal member is not implemented which IS NOT RECOVERABLE. This seems just wrong.
b) B is underlined although the problem is obviously that I am trying to inherit from A! Compiler should tell me that I am doing something wrong immediately without any of checking accessibility modifiers of every member in base class.
c) It is great that I can restrict inheriting from my base class from another assembly! But what if I want to restrict inheritance from my class which does not need any internal member? Then I am forced to add internal abstract member which is never used! I don't understand why people who created C# language just didn't make another keyword closed or whatever other name which would forbid inheritance from another assembly.
d) Adding internal member should not be breaking change outside of current assembly. I strongly believe in that. That's why if I designed C# I would prefer closed keyword. My feeling is that internal abstract members were not implemented properly.
How to define a must inherit class? in C#
You mark the class as abstract (this is the C# analogue to the VB.NET Must Inherit).
This will ensure it can't be instantiated directly.
From the linked MSDN article:
The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.
(emphasis mine)
Use the abstract modifier.
public abstract class MyClass()
{
...
}
You can define a class as abstract, or give it a protected-only constructor. abstract is better.
If u want to create a class, that has to be inherited, you'll need to mark it with the abstract modifier.
public abstract MyClass
{
}
It's not possible enforse needness of derivation or implementation in code, if that was a question.
But:
You can define an interface to force consumer to implement it.
Or you can define abstract class with only abstract members to force consumer to override all of them.
Hope this helps.
An interface would be best.
If you need to simulate the functionality , and its not a requirement that it fail at compile time...
define a method in the base class. Throw a an exception as the only line in the implementation. You might want to make the message very very clear about what the problem is.
override the method in the super class(es) and implement them.
If you fail to implement in a super class, you will get the exception.
Not perfect, but say you are trying to port code from vb.net... this could work.
In a namespace, is it possible to provide an alias for a class? And if not, why not?
By example, if I had several libraries of things that were derived from a contained, but named base class, but wanted to alias that as "BaseClass", while retaining its actual class name (i.e. "HtmlControl").
Then consumers could always come along and extend from HtmlControls.BaseClass, without having to figure out which class it really comes from.
using SomeClass = Large.Namespace.Other.FunkyClass;
class Foo : SomeClass
{
}
There really isn't an ideal way to do this in C#/.NET. What you can do is have a public BaseClass that inherits from an internal class. You can change this inheritance internally without breaking your consumers as long as the interface to the class remains intact.
public class PublicBaseClass : SomeInternalClass {
}
Consumers inherit from PublicBaseClass, and as long as you are careful, you can change what SomeInternalClass is as you wish.
You could create a dummy class that just inherits HtmlControl without adding any other functionality:
public class BaseClass : HtmlControl {}
The closest I know of is to customize your using statement:
using BaseClass = HtmlControls.BaseClass;
This is normally used to avoid ambiguity between classes with the same name in different used namespaces, without having to fully qualify one or the other. Your devs would have to include it in every code file, so probably not a good solution for what you're doing.
As far as deriving from BaseClass without knowing what you are actually deriving from, not possible. The compiler must, at some level, know what and where the parent class is, meaning it must be statically defined somewhere in code.
I have got a abstract class which is implementing 3 interfaces.
public abstract class ServiceBaseCore<Entity, EntityKey> : MarshalByRefObject, IComponentService, IEntityProvider<Entity, EntityKey>
where Entity : IEntityId<EntityKey>, new()
where EntityKey : IEntityKey, new()
{
// Provided functionality/body to some methods of interface
}
Problem: i am getting error that my abstract class is not providing implementation (definition/body) to functions of interface, where as what i read is that "if a class is abstract than there is no need to provide body to all/any functions of interface its implementing".
Note: the code was generated by codeSmith even though its showing error.
please tell me where i am wrong and what i am missing.
Thanks
Just create some abstract functions, and the compiler will stop complaining:
public abstract void MyMethodDeclaredInTheInterface();
EDIT: To speed up the process, just move the caret on the interface name in your abstract class, then ctrl + . and select "Implement interface YourInterface".
Then a little search and replace over the NotImplementedException should do the trick.
Create abstract methods for the interface. Otherwise, the class doesn't actually necessarily implement those methods in any way, even though derived classes might (the derived versions wouldn't be available to the base via vtables and therefore could not fulfill the interface contract). That would violate the idea behind interfaces.
Note: it's late and I'm tired, so I might be wrong about the rationale. But adding abstract methods for the methods required by the interfaces will take care of the problem.
You should be able to right click on the Interface name (near MyClass : IMyInterface) to see the context menu, and then choose 'Implement Interface'. Visual Studio will create all the required methods and properties to satsify the interface.
You may try some IDE to save much of your time. I know exactly, that Eclipse can do this automatically.
I usually make a base class abstract to give the signal this is a base class - you cannot instantiate me! even if there are no abstract methods in it.
Furthermore, I always make the base class constructor protected, although there's no real functional need to do that - I just want to make another point that this is a base class - you cannot instantiate me!
Am I jumping through hoops in doing that? What do you do?
It seems a reasonable thing to do, yes. There'll be no functional difference between the constructor being public or being protected, but making it protected gives a clearer indication of the intended use.
I'm not sure that you should set the class as abstract if there are no abstract methods. If it has a full implementation of the functionality that is expected of the classes derived from it, why not let it be instantiated and used as is? If this is just a way to share functionality across a range of classes then a composition based design may be more appropriate ie. have each 'derived' class reference this class rather than derive from it.
Is there a particular scenario you have in mind where this is could be an appropriate design?
Edit
The only scenario I have found where an abstract class with no abstract methods makes sense is when the abstract class is partially implementing and interface. The derived classes are required to complete the implementation.
Link to example (edit: site gone, This blog post seems to be a copy of the content)
In an abstract class, there's no difference between a public or protected constructor. As a matter of fact, I believe the compiler should give a warning or error when defining the constructor of an abstract class as public. Too bad it doesn't.