Ok, back to basics. I am wondering how to correctly overload a method with a params
argument.
Here's my scenario. I start with my regular method:
public void MyMethod(MyObject mo)
{
// method body
}
And I created an overload for it that looks like this:
public void MyMethod(MyObject mo, params string[] fields)
{
// new method body
MyMethod(mo);
}
The obvious intention is for MyMethod(new MyObject());
to execute the original method and MyMethod(new MyObject(), "field0"/*, etc...*/);
to execute the overloaded method. But that isn't what I'm finding to be the case.
What actually happens is that MyMethod(new MyObject());
executes the overloaded method!
I don't understand that. In this type of scenario, how would I execute the original method?
UPDATE with actual code
Okay, so here is the actual code that produces the described behavior.
Class1Base.cs:
public class Class1Base
{
public virtual void MyMethod(MyObject ob)
{
Console.WriteLine("Called Class1Base");
}
}
Class1.cs:
public class Class1 : Class1Base
{
public override void MyMethod(MyObject ob)
{
Console.WriteLine("called overridden method");
}
public void MyMethod(MyObject ob, params string[] fields)
{
Console.WriteLine("called OVERLOADED method");
}
}
MyObject.cs:
public class MyObject
{
public int Id { get; set; }
public string Description { get; set; }
}
Then, when I execute this code in this fashion:
var myClass = new Class1();
var myObject = new MyObject();
myClass.MyMethod(myObject);
myClass.MyMethod(null);
myClass.MyMethod(null, "string");
The console shows:
called OVERLOADED method
called OVERLOADED method
called OVERLOADED method
I would have expected it to show:
called overridden method
called overridden method
called OVERLOADED method
Why doesn't it?