VB.NET,对象是通过函数引用返回的 [英] VB.NET, Is Object Returned by Reference from Function

查看:141
本文介绍了VB.NET,对象是通过函数引用返回的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一个相当普遍的问题,但是我在任何地方都找不到一个简单的答案.

This should be a fairly common question, but I haven't found a straightforward answer anywhere.

如果我在VB.NET中的函数中实例化一个对象并将其返回,则它是按引用还是按值返回它. IE浏览器-如果我写这样的话,我应该担心性能吗?

If I instantiate an object within a function in VB.NET and return it, does it return it by reference or by value. IE - should I be worried about performance if I write something like this:

Public Function ret_obj_func() As big_object
    Dim ret_obj As New big_obj(<lots of stuff>)
    Return ret_obj
End Function

如果我从其他地方调用此函数,它将在ret_obj中实例化该对象,然后创建一个深层副本以将副本传递回调用方,还是仅传递回引用?

If I call this function from somewhere else, will it instantiate the object in the ret_obj and then create a deep copy to pass back a copy to the caller, Or will it just pass back a reference?

推荐答案

它只是传回一个引用(假设big_obj是一个类).我在这里不使用通过引用"一词,因为在参数传递方面它具有微妙的含义-但假设big_obj是一个类-引用类型-ret_obj的值是一个引用,然后该引用将返回.

It just passes back a reference (assuming big_obj is a class). I wouldn't use the term "by reference" here, as that has a subtly different meaning when it comes to parameter passing - but assuming big_obj is a class - a reference type - the value of ret_obj is a reference, and that reference will be what's returned.

从VB的角度来看,我没有任何文章,但是如果您乐于看C#,您可能会发现这些文章很有用:

I don't have any articles on this from a VB perspective, but if you're happy to look at C#, you may find these articles useful:

  • Reference and value types
  • Parameter passing in C#

这篇关于VB.NET,对象是通过函数引用返回的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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