Create class with Property - CSharp Custom Type

CSharp examples for Custom Type:Property

Description

Create class with Property



class Invoice
{
    private int quantityValue;
    private decimal priceValue;

    public int PartNumber { get; set; }

    public string PartDescription { get; set; }

    public Invoice(int part, string description, int count, decimal pricePerItem)
    {
        PartNumber = part;
        PartDescription = description;
        Quantity = count;
        Price = pricePerItem;
    }

    public int Quantity
    {
        get
        {
            return quantityValue;
        }
        set
        {
            if (value > 0) // determine whether quantity is positive
            {
                quantityValue = value; // valid quantity assigned
            }
        }
    }

    public decimal Price
    {
        get
        {
            return priceValue;
        }
        set
        {
            if (value >= 0M) // determine whether price is non-negative
            {
                priceValue = value; // valid price assigned
            }
        }
    }

    public override string ToString() => $"{PartNumber,-5} {PartDescription,-20} {Quantity,-5} {Price,6:C}";
}

Related Tutorials