-1

I have the following class

public class MyClass
{
  public int ElementId {get; set;}
  public int? LowerBoundary {get; set;}
  public int? UpperBoundary {get; set;}

  public int  SpecificMethod() {}

  public void CommonMethod()
  {
    int expectedValue = SpecificMethod();
  }
}

CommonMethod() is the same for every instance of this class, but I'd like SpecificMethod() to be different for each one. This method should always return an int, but it could take 0, 1 or 2 parameters (which are its own values for LowerBoundary and UpperBoundary properties).

Is there a way to achieve this? Since the number of parameter is variable, I understand I can't make SpecificMethod to be a Func property.

  • 1
    If they are all the same type you can use a [`params`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params) – Crowcoder Jul 08 '20 at 23:04
  • 4
    Your question is unclear. If the `SpecificMethod()` method should depend on the values of the `LowerBoundary` and `UpperBoundary` properties, why do they need to be passed as parameters at all? Why not have the method just use the property values directly? Please fix your question so that it is clear what you actually want to _do_. Show examples of the method as you imagine it would work, explain what you've tried already to attempt to accomplish that, and what _specifically_ you are unable to get to work. – Peter Duniho Jul 08 '20 at 23:06
  • Does this answer your question? [Function with variable number of arguments](https://stackoverflow.com/questions/9784630/function-with-variable-number-of-arguments) – Chetan Jul 09 '20 at 00:25

1 Answers1

1

Perhaps you can use params keywords:

public class MyClass
{

  public int SpecificMethod(params int[] values)
  {
    switch (values.Length)
    {
      case 0:
        ...
        break or return ...;
      case 1:
        ...
        break or return ...;
      case 2:
        ...
        break or return ...;
      default:
        ...
        break or return ...;
    }
    //return ...;
  }
}

https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/params

You can also define three overloaded methods:

public class MyClass
{
  public int SpecificMethod()
  {
    return 0;
  }
  public int SpecificMethod(int value)
  {
    return 0;
  }
  public int SpecificMethod(int value1, int value2)
  {
    return 0;
  }
}

You can create as many as overloaded methods with any types you need.

https://learn.microsoft.com/dotnet/standard/design-guidelines/member-overloading

Perhaps you may set the method as private or protected if only used by the instance.

  • Uh, okay. So now, with the answer strictly documenting the `params` keyword, what is the point of adding a new answer when you should have voted to close as a duplicate? E.g. https://stackoverflow.com/questions/18194849/is-there-a-c-sharp-alternative-to-javas-vararg-parameters, https://stackoverflow.com/questions/9784630/function-with-variable-number-of-arguments, https://stackoverflow.com/questions/11808414/how-to-pass-different-numbers-of-parameters-in-a-same-function, etc. – Peter Duniho Jul 09 '20 at 00:06
  • Frankly, I'm not convinced that `params` is in fact the answer to the unclear question, but anyone who does think it is should consider this question to be a duplicate of the _many_ already-existing Q&A that directly address that scenario and should vote-to-close rather than posting a new answer that doesn't contribute any new information to the site. – Peter Duniho Jul 09 '20 at 00:07
  • You offer overloading only as a secondary suggestion. Even if that suggestion applied, it too has plenty of duplicates on the site already, and in greater detail than you provide here. E.g. https://stackoverflow.com/questions/9812084/how-do-i-define-a-method-which-can-accept-different-sequences-of-arguments. Never mind that you offer no suggestion as to how the different overloads would be called, how doing so is helpful to the scenario asked about, nor how the one-parameter overload would be distinguished depending on whether it was the `Upper...` or `LowerBoundary` property that is missing. – Peter Duniho Jul 09 '20 at 00:55