Is this possible in c# somehow? This is the exact case I need it for: He (the user) must not be able to create instances of this class. However, I want to be able to create instances of this class inside my project. (hence: internally) Other classes should be able to inherit from it. I want to force the user to only use the bigger Sound class. The big "BUT": The user must be able to use instances of this class if I give it to him, that's why I can't just make it internal and be done with it. Neither can I make it abstract, because I want to make copies myself. I thought about giving the user only interfaces like IChannel back, but that doesn't solve my problem that he shouldn't be able to make an instance of Channel. He will still be able to see the class if he looks for it, and if he is adventureous enough even try to create an instance. But as I mentioned before: I can't make it abstract because I need to make copies internally.
Appreciate your help!
And yes, you can declare abstract class without defining an abstract method in it. Once you declare a class abstract it indicates that the class is incomplete and, you cannot instantiate it. Hence, if you want to prevent instantiation of a class directly you can declare it abstract.
Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract .
An abstract class is a "partially implemented" class which other classes can inherit from, but if they do, they must provide their own implementations for any method in the abstract class that is not already implemented. An abstract class is defined using the abstract keyword.
An Abstract class can have access modifiers like private, protected, and internal with class members. But abstract members cannot have a private access modifier. An Abstract class can have instance variables (like constants and fields). An abstract class can have constructors and destructors.
You should be able to declare the constructor as internal and the class itself as public. That should (I think) give you what you want.
Shouldn't an internal
constructor solve the issue? That way only your assembly can create instances, but the user can still use instances without restrictions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With