Question

I have a method which takes last parameter optionally.

public static DataTable GetQueryResult<T>(string connectionString, string queryText, Dictionary<string, T> dicParameters = null)

When I try to call this method like:

DBOperations.GetQueryResult(myConnectionString, myQuery);

It says no overload for method 'GetQueryResult' takes 2 arguments.

This documentation explains that I can pass only needed parameters to this kind of method.

Regards

Was it helpful?

Solution

You must explicitly specify the T:

DBOperations.GetQueryResult<YourType>(myConnectionString, myQuery);

When you specify the dicParameters, the T is implicit:

var dicParameters = new Dictionary<string, YourType>();
DBOperations.GetQueryResult(myConnectionString, myQuery, dicParameters );

OTHER TIPS

In this case it is better to have an overload rather than a default parameter.

As you can see from the other answers, if you use a default parameter and the caller does not specify it, then they will need to specify the type of T in the call:

DBOperations.GetQueryResult<MyType>(myConnectionString, myQuery);

However, because the dictionary is not used if it is null (presumably!), then it is pointless for the caller to have to specify an arbitrary type just to call the function when not specifying a dictionary.

So you should introduce an overload which doesn't have the dictionary parameter at all, so the caller won't have that problem.

You can specify the type when calling the method

DBOperations.GetQueryResult<Person>(myConnectionString, myQuery);

The compiler can't guess what T is if you do not provide the last parameter.

Try calling your method with an explicit T:

DBOperations.GetQueryResult<Something>(myConnectionString, myQuery);
change the call from DBOperations.GetQueryResult(myConnectionString, myQuery); 
to e.g DBOperations.GetQueryResult<int>(myConnectionString, myQuery); or DBOperations.GetQueryResult<string>(myConnectionString, myQuery);  whatever data type you will be using for dictionary
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top