hyunsooPark
hyunsooPark

Reputation: 39

Selecing an overloaded method using 'Named arguments'

I want to use 'Named Arguments' feature as parameter.

There is two methods.

Class temp {
    public method(Class1 param);
    public method(Class2 param);
};

In case of above class, if I want to call method using 'named arguments'. I can call like method(param: 'some value'); and I want to give null in 'some value', so when I try like this,

method(param:null);

I see this message

The call is ambiguous between the following methods or properties: 'method' and 'method'

Then, I realized these two (Class1, Class2) are very similar.

So when I try to cast it like this,

method((Class1)param:null);

I failed with message below.

The name 'identifier' does not exist in the current context

So can 'cast' this 'param' parameter?

Thank you.

Upvotes: 1

Views: 70

Answers (2)

TheSkimek
TheSkimek

Reputation: 342

Why not seperate the two methods?

  • method1(Class1 param)

  • method2(Class2 param)

OR something like:

public method<T>(T param)
{
    if(param.GetType() == typeof(Class1))
    {
      //Do stuff for Class1
      Console.WriteLine("Class1");
    }
    else if (param.GetType() == typeof(Class2))
    {
      //Do stuff for Class2
      Console.WriteLine("Class2");
    }
}

Upvotes: 0

ediblecode
ediblecode

Reputation: 11971

You do it like this:

MethodName(param: (Class1)null)

Basically, you need to cast the null to your given type, not the parameter

Upvotes: 1

Related Questions