C#: force constructor inheritance - c#

I've read a lot of questions of people asking how to have implicit ctor inheritance, so to not have to copy them around.
This question is totally the opposite: why C# is not forcing me to do it anymore? I want the derived classes to be forced to implement the base constructors...but it's not happening. What am I doing wrong?
Base class
public abstract class LogicalDevice
{
private LogicalDevice()
{
}
protected LogicalDevice(string id)
{
}
}
Intermediate derived class
public abstract class Device : LogicalDevice
{
public Device(string ID)
: base("ID")
{
}
public Device(ConfigurationData configuration)
: base(configuration["ID"])
{
}
}
Final derived class
internal class CoffeMachineDevice : Device
{
public CoffeMachineDevice (ConfigurationData configuration)
: base(configuration)
{
}
}
Why the class CoffeMachineDevice compiles?

C# requires that every constructor in derived class must call a constructor in the base class. But there is no requirement that is must call each constructor of the base class.
That's why CoffeMachineDevice compiles.

Related

How to require an implementation of an abstract class in C#?

I want to build a class that would have a property, in which there is an instance of a class, which implements an abstract class. Here's and example.
public class MyClass {
public MyDerivedClassA derived;
public void mainClassUtility () {
derived.foo();
}
}
public abstract class MyAbstractBaseClass {
public abstract void foo();
}
public class MyDerivedClassA : MyAbstractBaseClass {
public override void foo(){
return;
}
}
public class MyDerivedClassB : MyAbstractBaseClass
{
public override void foo()
{
return;
}
}
Basically, I want to make sure the object I'm using is derived from an abstract class and implements all the methods I will need to use. There will be many implementations of the abstract class and depending on the current state of the program, MyClass might be using different implementations of the ABC. I want to write the program in a way, that no matter what implementation of the ABC is currently being used, there is a way to call it's methods by MyClass. What would be the best solution to this problem?
Unless I'm misunderstanding the question, you're pretty much there. Have MyClass expect a property of the abstract base class and you should be all set.
using System;
public class Program
{
public static void Main()
{
var myClassOne = new MyClass(new MyDerivedClassA());
var myClassTwo = new MyClass(new MyDerivedClassB());
myClassOne.mainClassUtility();
myClassTwo.mainClassUtility();
}
public class MyClass
{
public MyAbstractBaseClass Derived;
public MyClass(MyAbstractBaseClass derived)
{
Derived = derived;
}
public void mainClassUtility ()
{
Derived.foo();
}
}
public abstract class MyAbstractBaseClass
{
public abstract void foo();
}
public class MyDerivedClassA : MyAbstractBaseClass
{
public override void foo()
{
Console.WriteLine("I am MyDerivedClassA");
return;
}
}
public class MyDerivedClassB : MyAbstractBaseClass
{
public override void foo()
{
Console.WriteLine("I am MyDerivedClassB");
return;
}
}
}
How to require an implementation of an abstract class in C#?
You can not instantiate a abstract class - and thus can not use it for most cases. Except as variable/argument/generic type argument. You need to make a concrete (non-abstract) class that inherits from it. You can only use the abstract class as a variable/argument type. To guarantee that only stuff that inherits from it can be used there.
Basically, I want to make sure the object I'm using is derived from an abstract class and implements all the methods I will need to use.
Then use the abstract class as type argument. It means only instaces of the abstract class (of wich there can be no instance) or instances of classes that inherit from it (that somebody else writes) can be used at that place.
Note that Abstract classes and Interfaces overlap in nearly all uses. There is a miriad small differences, but I do not think they mater. The only big difference I can see, is one of exclusivity:
a class can implement as many Interfaces as it wants.
You can only inherit from one abstract class. that means it is for a primary, exclusive purpose. That way you prevent some dumb ideas, like someone trying to make a Windows Form that is also a DBConnection.

Is it a bad idea to initialize all derived member variables in base class?

I have a bunch of derived classes that inherit from a base class. Each derived class still has unique members. One of these derived classes needs to be able to access all the other derived class's members. My aim with this problem is to write as little code as possible.
Would it be sloppy or bad practice to initialize all the derived classes' members in the base class so that one of the derived classes can access those members, instead of initializing them in said derived class?
Proposed approach:
public class BaseClass {
public BaseClass() {
...
der1Initializer = Der1Initializer(new Factory1());
der2Initializer = Der1Initializer(new Factory2());
List initializers = new List(){ der1Initializer , der2Initializer };
der3Initializer = Der3Initializer(initializers);
}
Der1Initializer der1Initializer;
Der2Initializer der2Initializer;
Der3Initializer der3Initializer;
}
public class DerivedClass1 : BaseClass {
public SomeFunction {
der1Initializer.init();
}
}
public class DerivedClass2 : BaseClass {
public SomeFunction {
der2Initializer.init();
}
}
public class DerivedClass3 : BaseClass {
...
}
So that:
public class Der3Initializer {
public GroupInitializationFunction {
initializers[0].init(); //der1Initializer
initializers[1].init(); //der2Initializer
}
}
Instead of:
public class BaseClass {
public BaseClass() {
...
}
public class DerivedClass1 : BaseClass {
public DerivedClass1 {
der1Initializer = Der1Initializer(new Factory1());
}
public SomeFunction {
der1Initializer.init();
}
Der1Initializer der1Initializer;
}
public class DerivedClass2 : BaseClass {
public DerivedClass2 {
der2Initializer = Der2Initializer(new Factory2());
}
public SomeFunction {
der2Initializer.init();
}
Der2Initializer der2Initializer;
}
public class DerivedClass3 : BaseClass {
public DerivedClass3 {
List initializers = new List()
{
Der1Initializer(new Factory1()),
Der2Initializer(new Factory2());
};
der3Initializer = Der3Initializer(initializers);
}
...
Der3Initializer der3Initializer;
}
...
public class Der3Initializer {
public GroupInitializationFunction {
initializers[0].init(); //der1Initializer
initializers[1].init(); //der2Initializer
}
}
This is a vast oversimplification of the "problem". The purpose of this code is to reduce duplicate code and the need to reinitialize members that can be shared and to optimize performance.
I am aware that it isn't necessarily a good idea to give other classes access to members they aren't using.
I just thought this is an interesting problem regarding code separation vs. duplicate code.
I would not recommend initializing childs on the parent class as
Violates Open Closed Principle as the base class has the responsibility of initializing its children and there will always be the need to add new child classes and consequently modifying base class .
Violates Single Responsibility Principle as this is not the role for the base class (to initialize its childs )
Violates Inversion of Control as base class is tightly coupled to child class see this link

How to Inherit from Generic Parent

I have a parent Class
public class GenericRepository<TEntity> where TEntity : class
{
//Implementation
}
And I want to inherit from this class, but I can't seem to get it right,here are my attempts
public class CustomerRepository<Customer> : GenericRepository<Customer>
{
//implementation
}
Or this,
public class CustomerRepository<T> : GenericRepository<T> where T : new Customer()
{
}
Or this one
public class CustomerRepository<T> : GenericRepository<CustomerRepository<T>> where T : CustomerRepository<T>
{
}
No matter what I do, I get this error. Please show me how I can inherit from this class, classes share the same Namespace
Error 'GenericRepository' does not contain a constructor that takes 0 arguments CustomerRepository.cs
It sounds like you want a non-generic class inheriting from a generic one, like this:
public class CustomerRepository : GenericRepository<Customer>
{
}
If you want this to be a generic class that narrows the type of the generic parameter (only allows Customer or a derived type):
public class CustomerRepository<T> : GenericRepository<T>
where T : Customer
{
}
Regarding your compile-time error:
Error 'GenericRepository<Customer>' does not contain a constructor that takes 0 arguments
This means exactly what it says. You have not defined a constructor in your derived class, which means that a constructor is implicitly generated, as though you had typed this:
public CustomerRepository() : base() { }
However, the base class (GenericRepository<Customer>) does not have a constructor that takes no arguments. You need to explicitly declare a constructor in the derived class CustomerRepository and then explicitly call a constructor on the base class.
You don't need to repeat the type parameter in the deriving class, so:
public class CustomerRepository : GenericRepository<Customer>
{
//implementation
}
Is what you need.
It seems that your base class has no constructor without parameters, if so the derived class must declare a.constructor and call the base class constructor with parameter.
class MyBase { public MyBase(object art) { } }
class Derived : MyBase {
public Derived() : base(null) { }
}
In this example if you remove the ctor from Derived you get the same error.
Use can write as:
public class CustomerRepository : GenericRepository<Customer>
{
//implementation
}

Is it possible to prevent inheritance of an abstract class

Consider the code:
public abstract class Base
{
public Dictionary<int, string> Entities { get; private set; }
protected Base()
{
Entities = new Dictionary<int, string>();
}
}
public abstract class A : Base
{
public abstract void Update();
}
public abstract class B : Base
{
public abstract void Draw();
}
Is it possible to restrict classes (in the same assembly) from inheriting from Base, forcing them to inherit from either A or B?
To elaborate a little on my comment. This isn't supported, so the answer is no. The only contrived way of trying this that comes to mind (that obviously wouldn't work) would be to have a sealed abstract class, which is nonsensical, as neither A nor B, nor anything else, could then inherit either.
There is no discriminator that allows us to say, "sealed for", for instance.
The only way to prevent an inheritance is declare class like sealed.
public sealed abstract class A
{
}
but you will get compile error:
Abstract class can not be declared sealed.
The reason for this is that simply no any meaning in decalring abstract class and limiting its inheritance tree. Abstact class is an entity that targets to be expanded by someone by its definition.
The closest thing you can do, as far as I know, is the following:
public abstract class Base
{
internal Base()
{
// do stuff
}
}
Then move A, B, and Base into their own assembly. Anything outside the assembly can see Base, but cannot extend it directly because it can't access the constructor.
Or you could do a runtime check:
public abstract class Base
{
protected Base()
{
if (!(this is A || this is B))
// throw an exception
}
}
The only way round this that I can think of would be to have something like:
A.dll
internal abstract class Base
{
public Dictionary<int, string> Entities { get; private set; }
protected Base()
{
Entities = new Dictionary<int, string>();
}
}
B.dll
public abstract class A : Base
{
public abstract void Update();
}
public abstract class B : Base
{
public abstract void Draw();
}
C.dll
public class C : A
{
public void Draw()
{
}
}
and then to use [assembly: InternalsVisibleTo...] in the AssemblyInfo.cs of A.dll to make internal classes visible to B.ll.
Disclaimer: It's quite convoluted and I wouldn't recommend it, but I couldn't think of any way to satisfy your constraints.

How can I prevent a base constructor from being called by an inheritor in C#?

I've got a (poorly written) base class that I want to wrap in a proxy object. The base class resembles the following:
public class BaseClass : SomeOtherBase
{
public BaseClass() {}
public BaseClass(int someValue) {}
//...more code, not important here
}
and, my proxy resembles:
public BaseClassProxy : BaseClass
{
public BaseClassProxy(bool fakeOut){}
}
Without the "fakeOut" constructor, the base constructor is expected to be called. However, with it, I expected it to not be called. Either way, I either need a way to not call any base class constructors, or some other way to effectively proxy this (evil) class.
There is a way to create an object without calling any instance constructors.
Before you proceed, be very sure you want to do it this way. 99% of the time this is the wrong solution.
This is how you do it:
FormatterServices.GetUninitializedObject(typeof(MyClass));
Call it in place of the object's constructor. It will create and return you an instance without calling any constructors or field initializers.
When you deserialize an object in WCF, it uses this method to create the object. When this happens, constructors and even field initializers are not run.
If you do not explicitly call any constructor in the base class, the parameterless constructor will be called implicitly. There's no way around it, you cannot instantiate a class without a constructor being called.
At least 1 ctor has to be called. The only way around it I see is containment. Have the class inside or referencing the other class.
I don't believe you can get around calling the constructor. But you could do something like this:
public class BaseClass : SomeOtherBase
{
public BaseClass() {}
protected virtual void Setup()
{
}
}
public BaseClassProxy : BaseClass
{
bool _fakeOut;
protected BaseClassProxy(bool fakeOut)
{
_fakeOut = fakeOut;
Setup();
}
public override void Setup()
{
if(_fakeOut)
{
base.Setup();
}
//Your other constructor code
}
}
If what you want is to not call either of the two base class constructors, this cannot be done.
C# class constructors must call base class constructors. If you don't call one explicitly, base( ) is implied. In your example, if you do not specify which base class constructor to call, it is the same as:
public BaseClassProxy : BaseClass
{
public BaseClassProxy() : base() { }
}
If you prefer to use the other base class constructor, you can use:
public BaseClassProxy : BaseClass
{
public BaseClassProxy() : base(someIntValue) { }
}
Either way, one of the two will be called, explicitly or implicitly.
When you create a BaseClassProxy object it NEEDS to create a instance of it's base class, so you need to call the base class constructor, what you can doo is choose wich one to call, like:
public BaseClassProxy (bool fakeOut) : base (10) {}
To call the second constructor instead of the first one
I am affraid that not base calling constructor isn't option.
I ended up doing something like this:
public class BaseClassProxy : BaseClass
{
public BaseClass BaseClass { get; private set; }
public virtual int MethodINeedToOverride(){}
public virtual string PropertyINeedToOverride() { get; protected set; }
}
This got me around some of the bad practices of the base class.
constructors are public by nature. do not use a constructor and use another for construction and make it private.so you would create an instance with no paramtersand call that function for constructing your object instance.
All right, here is an ugly solution to the problem of one class inheriting the constructors of another class that I didn't want to allow some of them to work. I was hoping to avoid using this in my class but here it is:
Here is my class constructor:
public MyClass();
{
throw new Exception("Error: Must call constructor with parameters.");
}
OK now you were warned that it was ugly. No complaints please!
I wanted to force at least the minimal parameters from my main constructor without it allowing the inherited base constructor with no parameters.
I also believe that if you create a constructor and do not put the : base() after it that it will not call the base class constructor. And if you create constructors for all of the ones in the base class and provide the same exact parameters for them in the main class, that it will not pass through. But this can be tedious if you have a lot of constructors in the base class!
It is possible to create an object without calling the parameterless constructor (see answer above). But I use code like this to create a base class and an inherited class, in which I can choose whether to execute the base class's init.
public class MyClass_Base
{
public MyClass_Base()
{
/// Don't call the InitClass() when the object is inherited
/// !!! CAUTION: The inherited constructor must call InitClass() itself when init is needed !!!
if (this.GetType().IsSubclassOf(typeof(MyClass_Base)) == false)
{
this.InitClass();
}
}
protected void InitClass()
{
// The init stuff
}
}
public class MyClass : MyClass_Base
{
public MyClass(bool callBaseClassInit)
{
if(callBaseClassInit == true)
base.InitClass();
}
}
Here is my solution to the problem
using System;
public class Program
{
public static void Main()
{
Console.WriteLine(new Child().Test);
}
public class Child : Parent {
public Child() : base(false) {
//No Parent Constructor called
}
}
public class Parent {
public int Test {get;set;}
public Parent()
{
Test = 5;
}
public Parent(bool NoBase){
//Don't do anything
}
}
}
A simple elegant solution. You can change it according to your need.
Another simple solution from me:
class parent
{
public parent()
{
//code for all children
if (this.GetType() == typeof(child1))
{
//code only for objects of class "child1"
}
else
{
//code for objects of other child classes
}
}
}
class child1 : parent
{
public child1()
{}
}
// class child2: parent ... child3 : parent ... e.t.c

Categories

Resources