Q: Why do we use tuple in C#?
A: Basically, when we want to return multiple values like servicefee, markup, and coupon amount without using any class, then we can use a tuple to return all three values at once from a method. Here is the programming snapshot.
How to create a method
- public static (float servicefee, float discount, float couponamount) GetAppliedAmount(float totalservicefee, float totalDiscount, float totalcouponamount)
- {
- return (totalservicefee , totalDiscount, totalcouponamount);
- }
How to use this method
- Public void GetAmount()
- {
- var calculatedamount= GetAppliedAmount(totalservicefee, totalDiscount, totalcouponamount);
- float discount= calculatedamount.servicefee;
- float discount= calculatedamount.discount;
- float discount= calculatedamount.couponamount;
- }