Generic Method - Cannot implicitly convert type 'string' to T

前端 未结 4 1296
后悔当初
后悔当初 2021-01-15 04:41

May be a simple question..

I have an interface:

public interface ISanitizer
{
    T Sanitize(T data_);
}

And an implementi

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-15 05:26

    In your code you are returning a string from a generic type which could be anything.

    Change your code to this if you want a generic return type:

    public interface ISanitizer
    {
        T Sanitize(T data_);
    }
    
    public class BasicFilenameSanitizer : ISanitizer
    

    If you simply want to always return a string you only need to change the method return type:

    public interface ISanitizer
    {
        string Sanitize(T data_);
    }
    
    public virtual string Sanitize(T filename_)
    

提交回复
热议问题