Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better design for overloaded constructors?

Tags:

c#

oop

I've been playing around with constructors and noticed in most code overloaded constructors are:

public class ClassFirst
    {
        public string Name { get; set; }
        public int Height { get; set; }
        public int Weight { get; set; }

        public ClassFirst(string name, int height, int weight)
        {
            Name = name;
            Height = height;
            Weight = weight;

        }

        public ClassFirst(string name)
            : this(name, 0, 0)
        { }

        public ClassFirst(string name, int height)
            : this(name, height, 0)
        { }

    } 

Which I would call 'underloading' instead of overloading, because the added constructors chip away at the full constructor... and seems to be used much more than the way I intuitively want to overload, which is ->

 public class ClassSecond
    {
        public string Name { get; set; }
        public int Height { get; set; }
        public int Weight { get; set; }

        public ClassSecond(string name)
        {
            Name = name;

        }

        public ClassSecond(string name, int height): this (name)
        {

            Height = height;    
        }

        public ClassSecond(string name, int height, int weight): this (name, height)

        {
            Weight = weight;
        }
}

Why are the constructors is used like that? There must be advantages...

Answer: below is a great example of how to concisely write overloaded constructors in .net 4.0 using default parameters.

Previous Stack Overflow Answer: I found there is a previous question that addresses constructor overloads: C# constructor chaining? (How to do it?)

like image 985
Stephan Luis Avatar asked Sep 01 '25 10:09

Stephan Luis


1 Answers

Do the overloading - but supply the default parameters in the default constructor (or lowest level as the case may be):

public class ClassSecond
{
    public string Name { get; set; }
    public int Height { get; set; }
    public int Weight { get; set; }

    public ClassSecond(string name)
    {
        Name = name;
        Height = 100;
        Weight = 100;
    }

    public ClassSecond(string name, int height)
        : this(name)
    {
        Height = height;
    }

    public ClassSecond(string name, int height, int weight)
        : this(name, height)
    {
        Weight = weight;
    }
}

Due to the order of which constructors are called, you'll be setting the variables to their default values, and then later overriding them with the user-specified values. This is valid as long as your properties aren't executing some sort of logic against the setter.

That being said, as you've posted an answer I would assume you're okay with .Net 4 default parameters. In which case all of that could be replaced with:

public MyClass(string name = "Ben", int height = 100, int weight = 20)
{
    Name = name;
    Weight = weight;
    Height = height;
}

This will contain the functionality of all the overloads you've built within your question and then some.

Examples (all valid code that perform as you'd expect):

MyClass a = new MyClass();
MyClass b = new MyClass("bob");
MyClass c = new MyClass("bob", 100);
MyClass d = new MyClass("bob", 141, 300);
MyClass e = new MyClass("bob", weight: 300);
MyClass f = new MyClass(height: 50);
like image 164
McAden Avatar answered Sep 05 '25 07:09

McAden