模板和界面使用之间的区别?

时间:2013-06-12 10:44:30

标签: c# templates interface

我正在尝试编写一个函数,从一个类中调用一个从接口派生的函数(ISFAction)。

区别/哪个更好?

  public string Create<T>(ISFServer server, T action, string[] args) where T : ISFAction
  {
      string requestUrl = null; 
      string actionPart = action.GenerateAction(args);
      requestUrl += server.serverUri.ToString();
      requestUrl += "request.php?req=";
      requestUrl += actionPart;

      return requestUrl;
  }

我的其他版本:

  public string Create(ISFServer server, ISFAction action, string[] args)
  {
      string requestUrl = null; 
      string actionPart = action.GenerateAction(args);
      requestUrl += server.serverUri.ToString();
      requestUrl += "request.php?req=";
      requestUrl += actionPart;

      return requestUrl;
  }

什么更好?

1 个答案:

答案 0 :(得分:2)

第二个代码片段(没有泛型类型参数)更好,因为(正如您在第一个代码片段中所示)泛型类型参数是无用的;它完全没用过。这只是没有理由的代码复杂化。

相关问题