0

New to programming and was wondering if it is best practice to declare a default constructor in C# in your code even though I think that C# will do it for you. For instance what is the purpose of the following open constructor with no statements. The following example is something that I am following along with but don't really understand why the two constructor classes of OperationResult are chained? Why is the empty one even needed?

public class OperationResult<T>
{
    public OperationResult()
    {
    }

    public OperationResult(T result, string message) : this()
    {
        this.Result = result;
        this.Message = message;
    }

    public T Result { get; set; }
    public string Message { get; set; }
}

}

1
  • 1
    C# will only do it for you if you don't declare any other constructors. Commented Oct 10, 2020 at 8:31

3 Answers 3

1

If you don't declare any constructors C# will assume the default constructor with no arguments. So if you don't need any overloads for the constructor and also don't need to write code when new instance of the class is created, you don't need to declare a constructor. However if you have overloads for the constructor (if you declare a constructor with arguments) the constructor with no arguments will not be available unless you explicitly declare it. And in case you don't need any code inside it, it would be like the empty constructor you mentioned in your example.

Sign up to request clarification or add additional context in comments.

Comments

0

There is no need actually of declaring two constructors until & unless you want to use it for specific purpose. If you want to initialise all variables to some value you can write that in constructor.

Ex.

MyClass
{
    int sum;
    public MyClass()
    {
        sum = 0;
    }
}

Now when to use parameterised constructor?

Let's say you want to initialise the Object with some specified configuration, or let's say you want to use dependency injection with constructor.

Comments

0

In your case, you don't need to chain them. However, you would need to use a parameterless constructor if you want to use an object initializer:

var result = new OperationResult {Message = "Hello world"};

It is also useful when you have public set properties.

Chaining constructors are useful when you have logic that will be repeated at constructors, or when you have base initializations.

For example, consider, that we have the following:

public sealed class MyClass {
    public MyClass() {
         Tags = new List<string>();
    }

    public MyClass(string username) : this()
    {
        if(string.IsNullOrWhiteSpace(username))
            throw new ArgumentNullException(nameof(username));

        Username = username;
    }

    public string Username {get; set;}
   
    public List<string> Tags {get;set;}
}

Here we have a list identified as Tags, and it is initialized with a constructor that has no parameters.

If we call MyClass(string username) without including : this(), then the base constructor will not be triggered, and the list identified as Tags will be initialized with the default value null.

If we attempted to perform some kind of action to the Tags, an exception would be thrown such as, NullReferenceException.

For example, what if we wanted to perform an action such as get by index or call LINQ method, etc? Any type may be assumed since List<string> has not been initialized.

With that in mind, some third-party libraries require parameterless constructors.

For example, let's explore Newtonsoft Json. If we do not provide a parameterless constructor, and a constructor with the [JsonConstructor] attribute, deserialization will throw an error.

I am unsure if this is the case with the Entity Framework, but usually, if you need a parameterless constructor solely for that purpose then you will set it private.

Comments

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.