【问题标题】:Generic parameter in c#c#中的通用参数
【发布时间】:2012-04-10 08:50:31
【问题描述】:

我有这些课程:

interface Info{}

class AInfo : Info { }
class BInfo : Info { }

class SendInfo {
    static public f_WriteInfo(params Info[] _info) {

    }
}

class Test {
  static void Main() {
    SendInfo.f_WriteInfo( 
                        new[] { 
                            new AInfo(){ ... },
                            new BInfo(){ ... },
                       } );
// This will generate an error. 
// There will be need casting between new and [] like new Info[]
  }
}

有没有什么方法可以在不强制转换的情况下做到这一点?

喜欢:

class SendInfo {
    static public f_WriteInfo(params T _info) where T : Info {

【问题讨论】:

  • new Info[] 有什么问题?这不是铸造。从技术上讲,您不需要数组,您可以在使用 params 时提供逗号分隔的参数:WriteInfo(info1, info2);
  • 顺便说一句,params 关键字意味着您不必在调用方法时显式创建数组,您只需执行MyMethod(firstParam, secondParam, thirdParam);
  • 问题是new[]在这种情况下被解释为object[]
  • 另请注意,您提供的代码应类似于:params Info[] infos,使用 params 时请注意参数周围的数组语法。
  • @RonKlein:不,new[] 在这种情况下根本不会编译。数组类型必须是其中一个表达式的类型。

标签: c# inheritance where array-initialization


【解决方案1】:

将您的方法签名设置为:

static public f_WriteInfo(params Info[] _info) {}

然后这样称呼它:

SendInfo.f_WriteInfo(new AInfo(){ ... }, new BInfo(){ ... });

【讨论】:

    【解决方案2】:

    这很好用

    interface Info { }
    
    class AInfo : Info { }
    class BInfo : Info { }
    
    class SendInfo
    {
        public static void f_WriteInfo(params Info[] _info)
        {
        }
    }
    
    class Test
    {
        static void Main()
        {
            SendInfo.f_WriteInfo(new AInfo(), new BInfo());
        }
    }
    

    【讨论】:

      【解决方案3】:

      尝试:

      namespace ConsoleApplication1
      {
          interface Info{}
      
      public class AInfo : Info
      {
          public AInfo(){}
      }
      public class BInfo : Info { }
      
      class SendInfo {
          public static void f_WriteInfo(params Info[] _info) {
      
          }
      }
      
      
      class Program
      {
          static void Main(string[] args)
          {
              SendInfo.f_WriteInfo( 
                          new Info[] { 
                              new AInfo(),
                              new BInfo()
                         } );
          }
      }
      

      }

      【讨论】:

      • 我不想使用 Info 投射。我的意思是想像new[] { new AInfo(), new BInfo() } 这样使用。为此,我可以将where 表达式添加到某处吗?
      • 然后 SendInfo.f_WriteInfo(new AInfo(){ ... }, new BInfo(){ ... });是最好的
      • @DaveS 这绝对有效,这是编译器将您的解决方案翻译成的内容。
      • @uzay95 虽然其他答案更好,因为它们更容易阅读,但它们都会编译成完全相同的。不涉及选角。
      • @GrahamClark;道歉。您对编译器逻辑是正确的,但是我从不知道您可以以这种方式调用。我删除了我最初的评论。
      【解决方案4】:

      来自MSDN

      params 关键字允许您指定一个方法参数,该参数采用可变数量的参数。您可以发送以逗号分隔的参数声明中指定类型的参数列表,或指定类型的参数数组。您也可以不发送任何参数。

      所以你不需要在参数之前写new []

      我想下面的链接也会有用
      how-to-pass-a-single-object-to-a-params-object

      【讨论】:

        猜你喜欢
        • 2014-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-02
        • 1970-01-01
        • 2018-11-07
        • 2016-11-10
        • 2016-05-13
        相关资源
        最近更新 更多