ref参数 [英] ref parameter
问题描述
嗨组,
我有一个类(TheClass),带有来自外部DDL的静态方法。
private void Test(ref字节参数)
{
字节a = 0,b = 0;
param = 1;
TheClass .CallToDLLFunction(ref a,ref b);
param = 2;
}
private void Test2(ref byte param) )
{
字节a = 0,b = 0;
param = 1;
TheClass.CallToOtherDLLFunction (参考a,参考b);
param = 2;
}
....
v = 0
测试(参考v)
// v = 2,按预期
Test2(参考v )
// v = 1.没有异常抛出
....
------ ------
我不明白extern函数如何对ref参数的
分配产生任何影响。 />
有人可以解释一下这种行为吗?
谢谢。
Hi group,
I have a class (TheClass) with static methods from an external DDL.
private void Test(ref byte param)
{
byte a = 0, b = 0;
param = 1;
TheClass.CallToDLLFunction(ref a, ref b);
param = 2;
}
private void Test2(ref byte param)
{
byte a = 0, b = 0;
param = 1;
TheClass.CallToOtherDLLFunction(ref a, ref b);
param = 2;
}
....
v=0
Test(ref v)
// v = 2, as expected
Test2(ref v)
// v = 1. There is no exception thrown
....
------------
I don''t understand how a extern function can have any influence on the
assignment of the ref parameters.
Can someone explain this behaviour, please?
Thanks.
推荐答案
6月6日上午11:25,Mathijs< math ... @ ongeldig.nlwrote:
< snip>
On Jun 6, 11:25 am, Mathijs <math...@ongeldig.nlwrote:
<snip>
我不明白extern函数如何对ref参数的
赋值有任何影响。
有人能解释一下这种行为吗?
I don''t understand how a extern function can have any influence on the
assignment of the ref parameters.
Can someone explain this behaviour, please?
如果你在调试2之前和之后对其进行了一些其他类型的诊断,请调用CallToOtherDLLFunction,你看到了什么?我的猜测是
*可能*会以某种方式在堆栈上踩踏,但这只是猜测。
Jon
If you put some other kind of diagnostics in Test2 before and after
the call to CallToOtherDLLFunction, what do you see? My guess is that
it *may* be trampling on the stack somehow, but it''s only a guess.
Jon
Jon Skeet [C#MVP] schreef:
Jon Skeet [C# MVP] schreef:
如果你在
调用CallToOtherDLLFunction,你看到了什么?
If you put some other kind of diagnostics in Test2 before and after
the call to CallToOtherDLLFunction, what do you see?
感谢您的回复。我对C#没有经验,我不确定用其他类型的诊断是什么意思
。
平台顺便说一下,紧凑型框架。
你的意思是检查param = 2行是否真的到达了?
private void Test2(ref byte param)
{
byte a = 0,b = 0;
param = 1;
MessageBox.Show(" 1:" + param.ToString());
TheClass.CallToOtherDLLFunction(ref a,ref b);
MessageBox .Show(" 2:" + param.ToString());
MessageBox.Show(" 3:" + param.ToString());
param = 2;
}
运行Test2显示三个消息框,其中包含1:1,2:1,3:2(所以,内部
Test2,参数值为2)。但是,在调用Test2的代码中,
参数的值为1(在调用2之后)。
Thanks for your reply. I''m not experienced with C# and I''m not sure what
you mean with ''other kind of diagnostics''.
The platform is Compact Framework, by the way.
You mean to check if the line ''param = 2'' is actually reached?
private void Test2(ref byte param)
{
byte a = 0, b = 0;
param = 1;
MessageBox.Show("1:"+param.ToString());
TheClass.CallToOtherDLLFunction(ref a, ref b);
MessageBox.Show("2:"+param.ToString());
MessageBox.Show("3:"+param.ToString());
param = 2;
}
Running Test2 shows three messageboxes with 1:1, 2:1, 3:2 (so, inside
Test2, the param value is 2). However, in the code which calls Test2,
the value of the parameter is 1 (after calling 2).
我的猜测是
它*可能*在某种程度上践踏堆栈,但这只是猜测。
My guess is that
it *may* be trampling on the stack somehow, but it''s only a guess.
啊......不确定它是什么意思,但是我有办法解决它吗?
或者有什么问题我如何使用ref参数?
干杯,
Mathijs。
Ah... not sure what it means, but there there a way how I can solve it?
Or is there something wrong with how I use the ref parameters?
Cheers,
Mathijs.
Jon Skeet [ C#MVP] schreef:
Jon Skeet [C# MVP] schreef:
我的猜测是
它*可能*在某种程度上践踏堆栈,但它只是一个猜测。
My guess is that
it *may* be trampling on the stack somehow, but it''s only a guess.
我找到了原因,但我仍然不明白它如何影响
调用函数中参数的分配。 ..
是什么导致这个,是b实际上是一个字节数组。
字节a = 0
byte [] b =新字节[7];
TheClass.CallToDLLFunction(ref a,ref b [0]);
干杯,
Mathijs。
I found the cause, but still I don''t understand how it could influence
the assignement of parameters in the calling function...
What causes this, is that b is actually a array of bytes.
byte a = 0
byte[] b = new byte[7];
TheClass.CallToDLLFunction(ref a, ref b[0]);
Cheers,
Mathijs.
这篇关于ref参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!