通过ref或值传递 [英] Passing by ref or by value

查看:89
本文介绍了通过ref或值传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PARAFORMAT2是SendMessage将返回内容的结构。


是ref正确或因为只有一个指针正在通过

值?


假设我传递数据而不是接收它,


会改变上面的答案吗?


[DllImport(" user32.dll",


EntryPoint =" SendMessage",CharSet = CharSet.Auto)]

public static extern int SendMessage(IntPtr hwnd,int wMsg,int wParam,


ref PARAFORMAT2 pf2);


提前致谢

PARAFORMAT2 is a structure that SendMessage will return stuff in.

Is the "ref" correct or since only a pointer is being passed should it be by
value?

Suppose I was passing data rather then receiving it,

would that change the answer to the above?

[DllImport("user32.dll",

EntryPoint="SendMessage",CharSet=CharSet.Auto )]

public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam,

ref PARAFORMAT2 pf2);

Thanks in advance

推荐答案



只是我 <是ne ******** @ a-znet.com>在消息中写道

news:ef ************* @ TK2MSFTNGP10.phx.gbl ...

" Just Me" <ne********@a-znet.com> wrote in message
news:ef*************@TK2MSFTNGP10.phx.gbl...
PARAFORMAT2是SendMessage的结构将返回的东西。

是ref正确或因为只有一个指针被传递它应该是
的值?

假设我传递数据而不是接收它,

会改变答案到上面?

[DllImport(" user32.dll",

EntryPoint =" SendMessage",CharSet = CharSet.Auto)]

public static extern int SendMessage(IntPtr hwnd,int wMsg,int wParam,



提前致谢
PARAFORMAT2 is a structure that SendMessage will return stuff in.

Is the "ref" correct or since only a pointer is being passed should it be by value?

Suppose I was passing data rather then receiving it,

would that change the answer to the above?

[DllImport("user32.dll",

EntryPoint="SendMessage",CharSet=CharSet.Auto )]

public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam,

ref PARAFORMAT2 pf2);

Thanks in advance



我会为此拍摄,我相信其他人会解释它

更好/更远。

来自C#程序员参考:


结构类型是一种值类型......


这意味着通过值传递结构传递内容的副本,并且

禁止修改原始数据。

您说pf2在参数列表中作为SendMessage()m返回的
数据的存储库ethod。要做到这一点,ref是方法定义中必不可少的一部分,以使pf2的地址传递给

SendMessage()并允许SendMessage更改数据原来的

位置。

如果你想将一个结构传递给一个方法,只是为了给方法提供所需的信息,而无意修改

结构中的任何数据,然后传递值(无参考)将正常工作。

警告:结构可能很大,你应该考虑效率

与将大块数据传递给方法相关的权衡与

传递十六进制内存地址。

您可能还想查看帮助关于

ref和out关键字的使用和区别。


-

Peter [MVP Visual Developer]

所有行业的杰克,无人掌握。


I''ll take a shot at this, and I''m sure others will explain it
better/further.

From the C# Programmer''s Reference:

"A struct type is a value type..."

That means passing a struct by value passes a copy of the contents, and
disallows modification of the original data.
You stated that pf2 is in the parameter list to serve as a repository for
data returned by the SendMessage() method. To do that, ref is a necessary
part of the method definition to cause the address of pf2 to be passed to
SendMessage() and allowing SendMessage to alter the data at the original
location.
If you wanted to pass a struct to a method solely to supply needed
information to the method, with no intention to modify any data within the
struct, then passing by value (no ref) will work fine.
Caveat: Structs can be large, and you should consider the efficiency
tradeoffs associated with passing large blocks of data to your methods vs.
passing a hex memory address.
You might also want to check the help for the use of and differences between
the ref and out keywords.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.


告诉我我需要知道什么。非常清楚。


我以为ref可能意味着指针的价值可能会被改变。


谢谢

" Peter van der Goes" < p _ ********** @ toadstool.u>在消息中写道

news:uo *************** @ TK2MSFTNGP11.phx.gbl ...
That tells me what I needed to know. Very clearly.

I was thinking that by ref might have meant the value of the pointer might
be changed.

Thanks
"Peter van der Goes" <p_**********@toadstool.u> wrote in message
news:uo***************@TK2MSFTNGP11.phx.gbl...

"只是我 <是ne ******** @ a-znet.com>在消息中写道
新闻:ef ************* @ TK2MSFTNGP10.phx.gbl ...

" Just Me" <ne********@a-znet.com> wrote in message
news:ef*************@TK2MSFTNGP10.phx.gbl...
PARAFORMAT2是SendMessage将返回内容的结构。

是参考吗?正确或因为只有指针被传递
PARAFORMAT2 is a structure that SendMessage will return stuff in.

Is the "ref" correct or since only a pointer is being passed should it


是由

值?

假设我传递数据而不是接收它,<是否会改变上述答案?

[DllImport(" user32.dll",

EntryPoint =" SendMessage",CharSet = CharSet.Auto)]

public static extern int SendMessage(IntPtr hwnd,int wMsg,int wParam,



提前致谢

我会为此拍摄,我相信其他人会更好/更好地解释它。
来自C#程序员的参考:

结构类型是一种值类型......

这意味着按值传递结构传递副本内容,并且不允许修改原始数据。
您声明pf2在参数列表中作为SendMessage()方法返回的数据的存储库。为此,ref是方法定义的必要部分,以使pf2的地址传递给SendMessage()并允许SendMessage更改原始位置的数据。
如果你想将一个struct传递给一个方法,只是为了向方法提供所需的信息,而无意修改
结构中的任何数据,那么传递值(没有引用)保证工作正常。
警告:结构可能很大,你应该考虑与将大块数据传递给你的方法相关的效率
权衡与传递十六进制存储器地址相关联。
value?

Suppose I was passing data rather then receiving it,

would that change the answer to the above?

[DllImport("user32.dll",

EntryPoint="SendMessage",CharSet=CharSet.Auto )]

public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam,

ref PARAFORMAT2 pf2);

Thanks in advance

I''ll take a shot at this, and I''m sure others will explain it
better/further.

From the C# Programmer''s Reference:

"A struct type is a value type..."

That means passing a struct by value passes a copy of the contents, and
disallows modification of the original data.
You stated that pf2 is in the parameter list to serve as a repository for
data returned by the SendMessage() method. To do that, ref is a necessary
part of the method definition to cause the address of pf2 to be passed to
SendMessage() and allowing SendMessage to alter the data at the original
location.
If you wanted to pass a struct to a method solely to supply needed
information to the method, with no intention to modify any data within the
struct, then passing by value (no ref) will work fine.
Caveat: Structs can be large, and you should consider the efficiency
tradeoffs associated with passing large blocks of data to your methods vs.
passing a hex memory address.
You might also want to check the help for the use of and differences





-
Peter [MVP Visual Developer]
所有行业的杰克,无人掌握。


between the ref and out keywords.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.



我认为String是一种引用类型。我还认为我应该因此将值传递给b
$ b。这是正确的吗?


按价值传递参考类型意味着什么?

再次感谢


PS之前我写这个我想确认String是一个引用类型

但是在文档中找不到它。

Peter van der Goes < p _ ********** @ toadstool.u>在消息中写道

news:uo *************** @ TK2MSFTNGP11.phx.gbl ...
I think String is a reference type. I also think that I should therefore
pass it by value in which case a pointer is passed. Is that correct?

Passing a reference type by value means what?
Thanks again

PS before I wrote this I wanted to confirm that String was a reference type
but couldn''t find that in the doc.
"Peter van der Goes" <p_**********@toadstool.u> wrote in message
news:uo***************@TK2MSFTNGP11.phx.gbl...

"只是我 <是ne ******** @ a-znet.com>在消息中写道
新闻:ef ************* @ TK2MSFTNGP10.phx.gbl ...

" Just Me" <ne********@a-znet.com> wrote in message
news:ef*************@TK2MSFTNGP10.phx.gbl...
PARAFORMAT2是SendMessage将返回内容的结构。

是参考吗?正确或因为只有指针被传递
PARAFORMAT2 is a structure that SendMessage will return stuff in.

Is the "ref" correct or since only a pointer is being passed should it


是由

值?

假设我传递数据而不是接收它,<是否会改变上述答案?

[DllImport(" user32.dll",

EntryPoint =" SendMessage",CharSet = CharSet.Auto)]

public static extern int SendMessage(IntPtr hwnd,int wMsg,int wParam,



提前致谢

我会为此拍摄,我相信其他人会更好/更好地解释它。
来自C#程序员的参考:

结构类型是一种值类型......

这意味着按值传递结构传递副本内容,并且不允许修改原始数据。
您声明pf2在参数列表中作为SendMessage()方法返回的数据的存储库。为此,ref是方法定义的必要部分,以使pf2的地址传递给SendMessage()并允许SendMessage更改原始位置的数据。
如果你想将一个struct传递给一个方法,只是为了向方法提供所需的信息,而无意修改
结构中的任何数据,那么传递值(没有引用)保证工作正常。
警告:结构可能很大,你应该考虑与将大块数据传递给你的方法相关的效率
权衡与传递十六进制存储器地址相关联。
value?

Suppose I was passing data rather then receiving it,

would that change the answer to the above?

[DllImport("user32.dll",

EntryPoint="SendMessage",CharSet=CharSet.Auto )]

public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam,

ref PARAFORMAT2 pf2);

Thanks in advance

I''ll take a shot at this, and I''m sure others will explain it
better/further.

From the C# Programmer''s Reference:

"A struct type is a value type..."

That means passing a struct by value passes a copy of the contents, and
disallows modification of the original data.
You stated that pf2 is in the parameter list to serve as a repository for
data returned by the SendMessage() method. To do that, ref is a necessary
part of the method definition to cause the address of pf2 to be passed to
SendMessage() and allowing SendMessage to alter the data at the original
location.
If you wanted to pass a struct to a method solely to supply needed
information to the method, with no intention to modify any data within the
struct, then passing by value (no ref) will work fine.
Caveat: Structs can be large, and you should consider the efficiency
tradeoffs associated with passing large blocks of data to your methods vs.
passing a hex memory address.
You might also want to check the help for the use of and differences





-
Peter [MVP Visual Developer]
所有行业的杰克,无人掌握。


between the ref and out keywords.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.



这篇关于通过ref或值传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆