Extension Methods - DanielAceroJuarez/TMSWebAPI GitHub Wiki

Extension Methods

Extension Methods

Extension methods allow you to add new methods to existing types without modifying the original type.

ShipmentExtensions

Provides an extension method for calculating the shipping cost.

Implementation

public static class ShipmentExtensions
{
    public static decimal CalculateShippingCost(this Shipment shipment, decimal ratePerKg)
    {
        return shipment.Weight * ratePerKg;
    }
}

Usage Example

var shipment = new Shipment(1, "New York", "Los Angeles", DateTime.Now, 100);
var shippingCost = shipment.CalculateShippingCost(1.5m);
Console.WriteLine($"Shipping Cost: ${shippingCost}");