Calling one constructor from another in C# (constructor chaining)
Hi everyone! I'm a beginner C# developer and I've run into a problem. I have a class `Order` with several constructors that have different parameters. I want to avoid code duplication, but I can't figure out how to call one constructor from another.
Here's simplified code:
```csharp
public class Order
{
public int Id { get; set; }
public string CustomerName { get; set; }
public decimal Amount { get; set; }
public DateTime CreatedDate { get; set; }
// Constructor 1: all parameters
public Order(int id, string customerName, decimal amount, DateTime createdDate)
{
Id = id;
CustomerName = customerName;
Amount = amount;
CreatedDate = createdDate;
Console.WriteLine("Order created: " + id);
}
// Constructor
No answers yet.
Log in to leave an answer