C# Generics: If T is a return type, can it also be void? How can I combine these interfaces together?

前端 未结 2 1083
粉色の甜心
粉色の甜心 2021-01-03 20:57

I have the following interface that returns the generic parameter of type T using a callback...

public interface IDoWork
{
    T DoWork();
}
         


        
相关标签:
2条回答
  • 2021-01-03 21:11

    Unfortunately, they can't be combined.

    You can see this in the framework - that's why there is a separate Task and Task<T> class, for example.

    That being said, you can often share implementations in this type of scenario by using IDoWork<object> and passing null for values, etc.

    0 讨论(0)
  • 2021-01-03 21:35

    return type is a part of method signature, so T DoWork() and void DoWork() are different, and void is not a type and it is not a null. It is an indication there is nothing in evaluation stack on return from method.

    0 讨论(0)
提交回复
热议问题