【问题标题】:How to pass ref parameters in params如何在params中传递ref参数
【发布时间】:2013-07-18 05:54:11
【问题描述】:

我想将n 数量的参数传递给一个方法(引用和普通)。 这是我的源代码

static void testParams(params object[] parameters) 
  { 
      for (int index = 0; index < parameters.Length; index++) 
        {
           Console.WriteLine(parameters[index ].gettype();
        }
  }

当我用作时它工作正常

int i=0, j=0; 
double k=0.0;
testParams(i,j,k)

但我想要这样,

int i=0, j=0; 
double k=0.0;
testParams(i,j,ref k)

如何做到这一点,请帮助我...

【问题讨论】:

  • 简短的回答是你不能。你想用这个来完成什么?您提供的代码中没有任何内容表明您需要ref 参数。
  • @p.s.w.g :这只是一个示例代码。我需要将参数传递给可能内置于 LAB VIEW 的旧版 dll
  • @shf301 :不..它不是。谢谢
  • @Pramodh 您也可以尝试制作一个 C 函数,例如 testParams(char* x,...);然后使用 c# 调用它[DLLImport("your dll")]。但我不确定是否可以

标签: c# params ref


【解决方案1】:

你不能。如果你想通过引用传递参数,方法的定义中应该有一个 ref。

例如

static void Mymethod(ref int i)

可以被调用

int localvariable = 5;
Mymethod(ref localvariable);

但你的方法定义不能是

static void Mymethod(int i)

【讨论】:

    猜你喜欢
    • 2014-11-12
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 2014-07-31
    • 2016-12-07
    • 1970-01-01
    相关资源
    最近更新 更多