Using the "params" keyword In C# with a Practical Scenario

Using the "params" keyword In C# with a Practical Scenario

In one of my development projects, I had to call a method to perform a process on different types of objects. Each object required some unique additional processing while the majority of the steps were common across all objects. Instead of writing separate methods for each individual object, I tried a new approach using the "params" keyword.

Facts about params

  • params is a keyword used to specify a parameter array in a method.
  • It must be applied to the last parameter of a method.
  • only one params parameter is allowed in a method signature.
  • The length of params will be zero if no arguments will be passed.
  • 'params' is here since C# 1.0 (2002)

Sample Example

Lets think we have to structure a method for handling different kind of payments which varying their parameters based on the payment type. Consider we have two payment options such as PayPal and credit cards. That both payment types share common parameters, as well as some differences.

Lets Define a Class to Handle the payment process

public void ProcessPayment(string paymentType, string billerName, params object[] additionalParams)
{
   // here goes the common payment processing logic
   // now lets handle the specific logic
   switch (paymentType.ToLower())
   {
      case "creditcard":
         // here goes the credit card logic
         ProcessOfCreditCardPayment(additionalParams);
         break;

      case "paypal":
         // here goes the PayPal logic
         ProcessOfPayPalPayment(additionalParams);
         break;

      default:
         // handle other payment methods or Errors
         break;
    }
}

private void ProcessOfCreditCardPayment(params object[] creditCardParams)
{
   // credit card payment processing logic
}

private void ProcessOfPayPalPayment(params object[] payPalParams)
{
   // payPal payment processing logic
}        

To keep the modularity of the code I Braked down the payment process into separate methods based on payment type. Here the switch statement makes it easier to add new payment types in the future. If we need to introduce a new payment method, we can simply create a new method for it, keeping the existing logic untouched.

Lets see how to call to this method

//create a object of PaymentProcessClass
PaymentProcessClass paymentProcessClass = new PaymentProcessClass();

// if its a creditcard payment
paymentProcessClass.ProcessPayment("creditcard", "Biller name", "CardNumber", "ExpiryDate", "CVV");

// if its a paypal payment
paymentProcessClass.ProcessPayment("paypal", "Biller name", "PayPalUsername", "Password");        

I think this will be helpful for you when you're writing a more efficient and effective code.

Thank you.


object

In C#, object is a fundamental data type / Universal Base Type that serves as the base class for all other types. It is part of the Common Type System (CTS) in the .NET framework. The object type can represent any value since all types in C# implicitly derive from it including value types (e.g., int, double) and reference types (e.g., classes, arrays).


要查看或添加评论,请登录

社区洞察

其他会员也浏览了