realist
realist

Reputation: 2375

Giving default value to generic parameter in C#

I have two methods like below. And I want to collect these two method in one method.

When I did like below two method working perfectly

public static async Task<GridResult<List<T>>> GetGridResult<T, TSearchFilter>(
    this IQueryable<T> query, 
    QueryFilter<TSearchFilter> queryFilter) where TSearchFilter : class
{
}

public static async Task<GridResult<List<T>>> GetGridResult<T>(
    this IQueryable<T> query, 
    QueryFilter queryFilter)
{
}

So, I collecty to my methods to one method by giving default null value to QueryFilter<TSearchFilter> queryFilter .

But, when I did one method, then gave me error

public static async Task<GridResult<List<T>>> GetGridResult<T, TSearchFilter>(
    this IQueryable<T> query, 
    QueryFilter<TSearchFilter> queryFilter = null) where TSearchFilter : class
{
}

But I can't call my method by one parameter like query.GetGridResult(queryFilter). It give me error

'IQueryableExtensions.GetGridResult(IQueryable, QueryFilter)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

How can I fix this error?

public class QueryFilter<TSearchFilter> where TSearchFilter: class
    {
        public QueryFilter()
        {
            SearchFilter = (TSearchFilter)Activator.CreateInstance(typeof(TSearchFilter));
        }
        public string SortBy { get; set; }
        public bool IsSortAscending { get; set; }
        public int PageFirstIndex { get; set; }
        public byte PageSize { get; set; }
        public TSearchFilter SearchFilter { get; set; }
    }

    public class QueryFilter
    {
        public string SortBy { get; set; }
        public bool IsSortAscending { get; set; }
        public int PageFirstIndex { get; set; }
        public byte PageSize { get; set; }
    }

Upvotes: 0

Views: 109

Answers (3)

realist
realist

Reputation: 2375

I found the answer by help of @MariePichova's answer. I changed my QueryFilter class like below. And then everything worked.

public class QueryFilter<TSearchFilter> where TSearchFilter : class
{
    public QueryFilter()
    {
        SearchFilter = (TSearchFilter)Activator.CreateInstance(typeof(TSearchFilter));
    }
    public string SortBy { get; set; }
    public bool IsSortAscending { get; set; }
    public int PageFirstIndex { get; set; }
    public byte PageSize { get; set; }
    public TSearchFilter SearchFilter { get; set; }
}

public class QueryFilter : QueryFilter<EmptySearchFilter>
{ }

public class EmptySearchFilter
{ }

Upvotes: 1

Marie Pichova
Marie Pichova

Reputation: 44

Because you're trying to call a method with generic parameter QueryFilter<TSearchFilter> queryFilter with the value of non-generic class QueryFilter, which is not anyhow related to the generic one.

Edit: If you wish to use a single method, then QueryFilter must be derived from QueryFilter<TSearchFilter>.

Upvotes: 1

NotFound
NotFound

Reputation: 6157

As null has no type it can't determine what the type of TSearchFilter is. You could call the method by specifying the types e.g. query.GetGridResult<string, object>(queryFilter), so that even though you might not use the object null it still is valid in your context.

I would honestly just keep it 2 seperate methods and have one call the other more detailed one.

Upvotes: 2

Related Questions