Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable parameterless constructor in C#

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?

like image 592
Carlos Liu Avatar asked Jun 08 '11 01:06

Carlos Liu


People also ask

How is a Parameterless constructor defined?

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.

Can constructors be Parameterless?

When a constructor is declared without any parameter or argument, then it is called a parameter-less constructor.

Why can struct have Parameterless 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.

Why static constructor is Parameterless?

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.

Do new() and struct type parameters require a parameterless constructor?

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.

What is a parameterless struct in implicitly?

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.

What happens if a constructor has a parameter with a default?

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;}}

Can you declare a default constructor for a struct?

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.


1 Answers

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) { }
}
like image 50
Anthony Pegram Avatar answered Oct 29 '22 05:10

Anthony Pegram