© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•11mo ago•
14 replies
Trapyy

✅ When to use each constructor?

public class InventorySlot(Item item, int quantity)
{
    public Item Item { get; } = item;
    public int Quantity { get; private set; } = quantity;
}
public class InventorySlot(Item item, int quantity)
{
    public Item Item { get; } = item;
    public int Quantity { get; private set; } = quantity;
}

or
public class InventorySlot
{
    public Item Item { get; } 
    public int Quantity { get; private set; }

    public InventorySlot(Item item, int quantity)
    {
        Item = item;
        Quantity = quantity;
    }
}
public class InventorySlot
{
    public Item Item { get; } 
    public int Quantity { get; private set; }

    public InventorySlot(Item item, int quantity)
    {
        Item = item;
        Quantity = quantity;
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

how to use private constructor?
C#CC# / help
4y ago
✅ Difficulty understanding Inheritance and where to use Constructor
C#CC# / help
3y ago
Converting the simple constructor to primary constructor
C#CC# / help
3y ago