ByVal和ByRef之间的区别? [英] Difference between ByVal and ByRef?

查看:135
本文介绍了ByVal和ByRef之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别?我总是使用ByVal,但是,我真的不知道我应该什么时候以及什么时候不... ...

>如果你传入一个引用,当你修改方法中的值时,调用网站中的变量也将被修改。



如果你传递值,那就是就像在该方法中创建另一个变量一样,所以即使您修改了它,原始变量(在调用站点上)也不会更改其值。



所以,的确,你通常应该把变量作为价值。如果您有明确的需要,只能作为参考传递。


What is the difference? I always use ByVal, but, I don't really have a good idea of when should I and when not...

解决方案

If you pass in a reference, when you modify the value in the method, the variable in the call site will also be modified.

If you pass value, it's the same as if another variable is created at the method, so even if you modify it, the original variable (at the call site) won't have its value changed.

So, indeed, you should usually pass variables as value. Only pass as reference if you have an explicit need to do so.

这篇关于ByVal和ByRef之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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