0

I was writing a class with two diffrent constructor overloads:

public class MyClass
{
    public MyClass(params string[] Files)
    {

    }
    public MyClass(string Path, params string[] Files)
    {

    }
}

After I wrote it, my code got me confused that how should I know that if the variable Path is given as argument or not. There is no error in program but I'm wondering for example if I run the following code, how to understand that Path is given or not?

MyClass x = new Myclass("a","b","c");

it can be either

Path = "a";
Files = string[]{"b","c"};

or just

Files = string[]{"a", "b", "c"};
d4Rk
  • 6,622
  • 5
  • 46
  • 60
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • 2
    If it's confusing, best not to code your constructors that way. – sstan Feb 12 '16 at 21:47
  • well what would you suggest? – Ashkan Mobayen Khiabani Feb 12 '16 at 21:48
  • 1
    Alternatives: Use either an explicit array instead of `params` (e.g. `new MyClass("a", new[] {"b", "c"})` or make your constructor private and use two shared methods with different names to create instances. – Heinzi Feb 12 '16 at 21:49
  • Personally, I would redesign it using @Henzi's 2nd suggestion. It will remove all confusion. And when he said *shared* methods, I guess he meant *static* methods in C# terminology. – sstan Feb 12 '16 at 21:51

1 Answers1

0

Try using named parameters:

MyFunction(parameter1: value1, parameter2: value2);

Felipe Cruz
  • 1,119
  • 10
  • 18