abstract class CAbstract
{
private string mParam1;
public CAbstract(string param1)
{
mParam1 = param1;
}
}
class CBase : CAbstract
{
}
For the class CBase, it should be initialized by providing the parameter, so how to disable the parameterless constructor for CBase class?
A constructor that takes no parameters is called a parameterless constructor. Parameterless constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new . For more information, see Instance Constructors.
When a constructor is declared without any parameter or argument, then it is called a parameter-less constructor.
Although the CLR allows it, C# does not allow structs to have a default parameter less constructor. The reason is that, for a value type, compilers by default neither generate a default constructor, nor do they generate a call to the default constructor.
When a data member is shared among different instances it is imperative that data should be consistent among all the instances of the class. And also there is no way to call static constructor explicitly. Therefore the purpose of having a parameterized static constructor is useless.
No change from C#9. The new () and struct type parameter constraints require the parameterless constructor to be public if defined (see Satisfying constraints - §8.4.5 ). The compiler assumes all structs satisfy new () and struct constraints. No change from C#9.
implicitly has a parameterless instance constructor which always returns the value that results from setting all value type fields to their default value and all reference type fields to null. A parameterless instance struct constructor must be declared public. Non-public constructors are ignored when importing types from metadata.
Now if you have a constructor with a parameter taking a default value, this rule doesn’t apply anymore. In the following example, the parameterless contructor is not automatically provided: publicclassFoo{privatestringBar;publicFoo(stringbar=null){Bar=bar;}}
Structs can declare constructors, but they must take parameters. It is an error to declare a default (parameterless) constructor for a struct. Struct members cannot have initializers. A default constructor is always provided to initialize the struct members to their default values.
If you define a parameterized constructor in CBase
, there is no default constructor. You do not need to do anything special.
If your intention is for all derived classes of CAbstract
to implement a parameterized constructor, that is not something you can (cleanly) accomplish. The derived types have freedom to provide their own members, including constructor overloads.
The only thing required of them is that if CAbstract
only exposes a parameterized constructor, the constructors of derived types must invoke it directly.
class CDerived : CAbstract
{
public CDerived() : base("some default argument") { }
public CDerived(string arg) : base(arg) { }
}
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