Can I force descendants to have a parameterless constructor? - c#

I am trying to create a generic factory-pattern-like mechanism.
The factory will be like:
public class APlugin<ActionType> where ActionType : IAction
{
// create a new action. Note: ActionType should contain
// an empty constructor
public ActionType CreateAction()
{
return Activator.CreateInstance<ActionType>();
}
}
Descendants of IAction might hide the parameterless constructor and this will cause the factory to fail.

You can ensure class has constructor without arguments by providing generic constraint where T : new(). It will affect type T only, though. Containing class will be unaffected, so you can make sure ActionType in your case has said constructor, but you can't enforce it on any class inheriting from APlugin<T>.

You can't force an empty constructor or any type of constructor on derived types.
Constructors do not get inherited, but do get called.
If not chaining to the base constructor (using the : base() syntax), the default base constructor will be called implicitly.
From Using Constructors on MSDN:
In a derived class, if a base-class constructor is not called explicitly using the base keyword, then the default constructor, if there is one, is called implicitly.

Pretty late but i think this is worth knowing..
You can force constructors with 0 parameters on non-abstract classes. This is a type constraint to use whenever you want to enforce those constraints on a class structure. It requires you to specify the type that implements the interface which might be found disturbing. That's the price if you want some neat type safety.
public interface IEmptyConstructor<TThis> where TThis : IEmptyConstructor<TThis>, new() {}
for example a singleton structure
public abstract class Singleton<TThis> : ISingleton where TThis : Singleton<TThis>, new() { /* some singleton madness */ }

Related

abstract class with more then one parameterized constructor and derived class with parameterized constructor

public class ParentBaseClass
{
public ParentBaseClass()
{
// Parameter less constructor
}
public ParentBaseClass(string type)
{
// single parameter constructor
}
public ParentBaseClass(Entity model)
{
// entity type constructor
}
public ParentBaseClass(string type, bool IsNewEntity)
{
// two parameter constructor
}
public ParentBaseClass(string type, bool IsNewEntity, Entity model)
{
// three parameter constructor
}
}
public class ChildClassFirst : ParentBaseClass
{
public ChildClassFirst() : base("Customer", false)
{
// implementation of 4th constructor having string, bool parameters.
}
}
in child class I implemented only one constructor of parent class having two parameters. why there no need to implement other constructor? can anyone explain me? please consider other constructor may have MVC Model class or other entity class of dbContext.
Every child class calls it's base class constructor, if there is a parameter-less constructor and we haven't implemented any parameter based constructor in it, the parameter-less is automatically called, but like in your case you are specifying which constructor to be used when ChildClassFirst is instantiated, so now the calling code has only access to this parameter-less constructor to instantiate an object of ChildClassFirst which in turn will instantiate the parent class object using the parameter based constructor which is called using base().
In short, Child class only needs to specify one base class construction which would be called during instantiation of child class, and when there is none specified the default would be parameter-less constructor which would get called, but remember the parameter-less only exists if we have no parameterized constructor specified in the class.
Abstract classes are designed for inheritance by other classes, usually more than one. Different derived classes may need to construct their abstract base class in slightly different ways. Hence, an abstract class can supply different constructors for different derived classes.
Non-abstract derived classes, on the other hand, are designed to be instantiated by the class users. User construction needs may be completely different from the needs of the class itself, so derived classes provide their own constructors.
Therefore, the two sets of constructors are completely independent of each other: they serve different needs. Derived classes can provide more or fewer constructors, and the signatures of their constructors may be similar or completely different. This is exactly the case in your code: ChildClassFirst provides a parameterless constructor, while its abstract base class provides two constructors with different sets of parameters. As long as you provide any constructor in the derived class, C# compiler is fine with your code.

Identical constructor in child and parent c# [duplicate]

I have a base class Character which has several classes deriving from it. The base class has various fields and methods.
All of my derived classes use the same base class constructor, but if I don't redefine the constructor in my derived classes I get the error:
Error: Class "child class" doesn't contain a constructor which takes this number of arguments
I don't want to redefine the constructor in every derived class because if the constructor changes, I have to change it in every single class which, forgive any misunderstanding, goes against the idea of only writing code once?
You can use the following syntax to call the base class constructor from the classes that derive from it:
public DerivedClass() : base() {
// Do additional work here otherwise you can leave it empty
}
This will call the base constructor first, then it will perform any additional statements, if any, in this derived constructor.
Note that if the base constructor takes arguments you can do this:
public DerivedClass(int parameter1, string parameter2)
: base(parameter1, parameter2) {
// DerivedClass parameter types have to match base class types
// Do additional work here otherwise you can leave it empty
}
You can find more information about constructors in the following page:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors
In a derived class, if a base-class constructor is not called explicitly by using the base keyword, the default constructor, if there is one, is called implicitly.
You do have to redeclare constructors, because they're effectively not inherited. It makes sense if you think of constructors as being a bit like static methods in some respects.
In particular, you wouldn't want all constructors to be automatically inherited - after all, that would mean that every class would have a parameterless constructor, as object itself does.
If you just want to call the base class constructor though, you don't need to write any code in the body of the constructor - just pass the arguments up to the base class as per Waleed's post.
If your base class starts requiring more information, it's natural that you should have to change all derived classes - and indeed anything calling the constructors of those classes - because they have to provide the information. I know it can seem like a pain, but it's just a natural consequence of what constructors do.
I had the same problem, and I solved it by replacing my constructor with a factory method like this:
A is the parent class.
public static T getChild<T>(int number) where T:A, new()
{
T child = new T();
T._number = number;
return child;
}
You can create a Child class with
Child b = A.getChild<Child>(2);
A kind of alternative could be to rely on a Dependency Injection container to initialize your objects, that way the that reference to the base class (could be the call to the base constructor or another initializer method) would "externalized" to the DI container.
I don't know if it makes sense to your case or not

What does the new() constraint do on a class definition?

I saw this code example and was wondering what the purpose of the new() constraint was:
public class Client<T> : IClient where T : IClientFactory, new()
{
public Client(int UserID){ }
}
That's called a "'new' constraint". Here's the documentation on it.
The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor. To use the new constraint, the type cannot be abstract.
(Emphasis mine)
Basically, you need it whenever you're creating a new T somewhere in the class, to ensure that you're only able to pass in things which the compiler can create a new instance of.
Client is a collection of T objects, and those T objects must implement the IClientFactory interface and have a public parameterless constructor.
new() means
The type argument must have a public parameterless constructor. When
used together with other constraints, the new() constraint must be
specified last.
Ref Generic Constraints on MSDN

new() in method

Having these classes:
public interface IDbContextFactory
{
DbContext GetContext();
}
public class Repo<T> : IRepo<T> where T : Entity, new()
{
protected readonly DbContext c;
}
public Repo(IDbContextFactory f)
{
c = f.GetContext();
}
What does the keyword new() (in class Repo<T>) do?
It means that the type T must expose a public, default (i.e. parameterless) constructor. That is, you will be able to construct an instance of T with new T(). It can expose other constructors as well, but this generic constraint makes the default one mandatory.
it means, the entity should have a parameterless public constructor.
see this.
When you use the where keyword on a generic definition you apply a type contraint to the generic paramater. The new() constraint declares that the type, T in this case, must have a default constructor. http://msdn.microsoft.com/en-us/library/sd2w2ew5.aspx
After reading your clarification disguised as an answer I thought I would try and help by clarifying a couple of things.
The code in your orginal question defines an interface that seemes to be used by a disembodied constructor. In between those two denfinitions you have defined a generic class which doesen't seem to do much.
Your question pertains to the generic class and the other two definitions are irrelavent to both the question and the answer.

Difference between default constructor and paramterless constructor?

A default constructor has no parameters. And nor does a constructor that you write with no parameters. So what is the ultimate difference in c#?
Added to this when you inherit a default constructor and a parameterless constructor are they exposed on the inheritting type exactly the same? Because my IOC container doesn't seem to think so. (Unity).
Cheers,
Pete
The "default" constructor is added by the C# compiler if your class does not contain an explicit instance constructor. It is a public, parameterless constructor. If you have created an explicit non-public, parameterless constructor, Unity will not be able to use it in the same way it would have used a public constructor (regardless of whether this was generated by the compiler, and regardless of whether it had parameters).
You can override the behaviour of the default constructor by creating a parameterless constructor. A common use of this is when you have a custom object as a member of your class and you need to initialize it with a default value when an instance of your class created.
A default constructor is a synonym for a parameterless constructor.
Adding to #Sachin Kainth and #Fredrik Leijon answers, in .NET (also in Java) constructors are not inherited.
You either explicitly call a base constructor or, by default, the base parameterless constructor is invoked.
If you don't add a constructor then a default constructor with no parameters is inserted for you. If you add a constructor (with or without parameters) no default constructor is generated
I'll let the code speak for itself:
public class ThisHasADefaultConstructor
{
}
public class ThisHasAParameterlessConstructor
{
public ThisHasAParameterlessConstructor()
{
}
}
Note that a default constructor is effectively a parameterless constructor that is automatically generated when you don't specify a constructor (with or without parameters).
Maybe you need the the parameterless constructor if you want to have more constructors. Let's say one without parameters and one with parameters.
if your class has only a constructor with parameters, you're are not allowed to use the parameterless constructor, unless you define one, in other words, no default parameterless constructor is provided if you define a custom constructor in your class.
MSDN states:
A constructor that takes no parameters is called a default constructor. Default constructors are invoked whenever an object is instantiated using the new operator and no arguments are provided to new.
So the default constructor does not have to be automatically generated. A hand-written, parameterless constructor is also the default constructor.
This actually makes sense when you look at derived classes:
class A
{
public A() { ... } // do something
public A(object o) { ... } // do something else
}
class B
{
public B() { ... }
}
Which A constructor is called while creating B? The default one.
Default constructor refers to a constructor without parameters, wheter created automatically or created by you.
A defualt constructor is created automatically if you don't declare any other instance constructor, with or without paramters. (declaring a static constructor won't avoid the defautl constructor to be automatically created).
The constructors of a hierarchy of classes are not inherited, but all the constructor on the inheriting chain are invoked when using the constructor of a derived class, starting from the root of the hierarchy. Unless you specify which constructor to invoke, the default constructor of each class is invoked.
public class Base
{
public Base()
{
}
}
public class Derived : Base
{
public Derived()
{
}
}
// invokes, the Base() construtor, and then the Derived() constructor
var d = new Derived();
You can also explicitly invoke a base class constructor, with or without parameters like so:
// Derived class explicitly invoking base class constructor.
public Derived() :base()
{
}
If you want, you can invoke a base class constructor with parameters like so:
public Derived(int j) :base(j)
or so
public Derived() :base(23)
In this last code sample, if you create a Derived object with the default constructor, the constructor invoked in the base class isn't the default one, but a constructor with an integer parameter, invoked with the parameter 23.
(of course, this constructor with parameter must exist in the Base class).
You can only invoke explicitly the constructor of the inmediate base class.
A default contructor is called implicitly by the comppiler, except if you have explicitly created a new one (with no parameters).

Categories