3

Is it possible to define an alternate parameterless constructor for a C# class?

In other words, I have a class Foo. I want to have a default constructer Foo() and another constructor SpecialFoo(). I don't mind if the SpecialFoo() constructor calls the Foo() constructor.

Can I do that?

2
  • What syntax do you propose to call this other constructor? var x = new Foo(); Commented Dec 21, 2013 at 2:20
  • Is there any particular reason why you're trying to do this? It sounds a bit like an X-Y Problem. Commented Dec 21, 2013 at 2:32

3 Answers 3

7

You can only have one constructor with given set of parameters, so you can't have two parameterless constructors.

You can have another public static Foo SpecialFoo method, which will be factory method and will return new instance of Foo class, but to use it you won't use new keyword:

class Foo
{
    public static Foo SpecialFoo()
    {
        return new Foo();
    }
}
var instance1 = new Foo();
var instance2 = Foo.SpecialFoo();
Sign up to request clarification or add additional context in comments.

Comments

3

Constructors, like methods, cannot have overloads with identical parameter lists. You can, however create a static factory method, like this:

public class Foo
{
    public static Foo SpecialFoo() {
         ...
    }
}

And call it like this:

var foo = new Foo();
var specialFoo = Foo.SpecialFoo();

An alternative is to use a separate constructor like this:

public class Foo
{
    public Foo(bool special) : this() {
         if (special)
         {
             ...
         }
    }
}

And call it like this:

var foo = new Foo();
var specialFoo = new Foo(true);

Of course, this doesn't actually qualify as an 'alternate parameterless constructor' but it has some benefits over the static factory. Primarily, you can use and extend it in an inherited class, which is something that the factory method does not allow*.

* Actually, you can, but you need to hide the base factory method with new, or you will get a warning, and it's generally bad practice to hide static members on base classes.

2 Comments

A static method works but it breaks the open-closed principle. You will need to modify the class every time you need a special function.
@DmitryS. I agree, a better solution if Foo is meant to be inheritable is to use a separate constructor. I just finished updating my answer with an appropriate solution.
1

You can do this:

public class Foo
{
    public Foo()
    {
        // do something
    }
}

public class SuperFoo : Foo
{
    public SuperFoo() : base() // call the Foo constructor - you do not have to call base explicitely, just a more verbose example
    {
        // do something else
    }
}

4 Comments

I don't like this because it doesn't scale.
What do you mean by scale? Multiple inheritance?
If you need 10 "special" constructors you have 10 extra types.
No, you define 10 subclasses. It is not much more expensive than having 10 methods. But factories with interfaces are probably a better way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.