是结构“传递按值”? [英] Are structs 'pass-by-value'?

查看:112
本文介绍了是结构“传递按值”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近想创建一个 Vector2 字段属性,只是认识到,按预期这是行不通的。

I've recently tried to create a property for a Vector2 field, just to realize that it doesn't work as intended.

public Vector2 Position { get; set; }

这让我无法改变其成员的值( X &放大器;

this prevents me from changing the values of its members (X & Y)

展望了这条信息,我读了创建一个属性一个 Vector2 结构仅返回原始对象的副本,而不是引用。

Looking up information on this, I read that creating a property to a Vector2 struct returns only a copy of the original object and not a reference.

作为一个Java开发这混淆了我。

As a Java developer this confuses me.

如果是按值传递在C#中的对象和当他们经过的参考?

是按值传递所有结构对象?

推荐答案

重要的是要认识到,在C#中一切都是​​由值过去了,除非您指定 REF 或<$在签名C $ C>退出。

It is important to realise that everything in C# is passed by value, unless you specify ref or out in the signature.

是什么让值类型(以及结构 S)与引用类型不同的是,值类型是直接访问,而引用类型是通过其引用访问。如果你传递一个引用类型到一个方法,的其参考的,而不是本身的价值,是按值传递。

What makes value types (and hence structs) different from reference types is that a value type is accessed directly, while a reference type is accessed via its reference. If you pass a reference type into a method, its reference, not the value itself, is passed by value.

这篇关于是结构“传递按值”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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