Maxime Recuerda
Maxime Recuerda

Reputation: 476

C# Constructors overloading useless brackets

I was writing a small class, and just realised something that triggered me. Here is what my code looks like:

class Foo
{
    public int Value;

    public Foo(Bar bar) : this(bar.Ga) { }
    public Foo(Baz baz) : this(baz.Bu) { }
    public Foo(Qux qux) : this(qux.Zo) { }
    public Foo(int val)
    {
        ...
        this.Value = val;
        ...
    }
}

I was wondering if we could replace the empty braces { }, something like:

public Foo(Bar bar) : this(bar.Ga);

Upvotes: 0

Views: 102

Answers (1)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391446

Unfortunately, no.

The syntax mandates a body for the constructor, and the empty braces are the best you can do.

Upvotes: 4

Related Questions