是什么在C#中引用类型和价值类型之间的区别? [英] What is the difference between a reference type and value type in c#?

查看:146
本文介绍了是什么在C#中引用类型和价值类型之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些人问我这个问题几个月前,详细我无法解释。什么是引用类型和C#中的值类型之间的区别?

Some guy asked me this question couple of months ago and I couldn't explain it in detail. What is the difference between a reference type and a value type in C#?

我知道值类型是 INT 布尔浮动等,并引用类型委托接口等,或这是错的,也?

I know that value types are int, bool, float, etc and reference types are delegate, interface, etc. Or is this wrong, too?

你能以专业的方式解释给我吗?

Can you explain it to me in a professional way?

推荐答案

您的例子是有些奇怪,因为在 INT 布尔浮动是特定类型,接口和代表们的各种 - 就像结构枚举是种值类型。

Your examples are a little odd because while int, bool and float are specific types, interfaces and delegates are kinds of type - just like struct and enum are kinds of value types.

我写的的引用类型的说明和值类型本文。我很乐意为你感到迷惑的任何位扩展。

I've written an explanation of reference types and value types in this article. I'd be happy to expand on any bits which you find confusing.

在TL; DR的版本是想到了什么特定类型的变量/ EX pression的价值。对于值类型,值是信息本身。为引用类型,该值是可为空,或者可以是导航到一个对象包含该信息的一种方法的参考。

The "TL;DR" version is to think of what the value of a variable/expression of a particular type is. For a value type, the value is the information itself. For a reference type, the value is a reference which may be null or may be a way of navigating to an object containing the information.

例如,想想一个变量的像一张纸。它可以有值5或写有假,但它可能没有我的房子......那就要有的路线的到我家。这些方向是一个参考的等效。特别是,两个人可以有不同的纸片含有相同的方向来我家的 - 如果一个人跟着这些方向和彩绘我的房子的红色,那么第二个人会看到变化了。如果他们都只是有我的房子的独立的图片在纸上,然后一个人着色他们的论文不会改变其他人的论文都没有。

For example, think of a variable as like a piece of paper. It could have the value "5" or "false" written on it, but it couldn't have my house... it would have to have directions to my house. Those directions are the equivalent of a reference. In particular, two people could have different pieces of paper containing the same directions to my house - and if one person followed those directions and painted my house red, then the second person would see that change too. If they both just had separate pictures of my house on the paper, then one person colouring their paper wouldn't change the other person's paper at all.

这篇关于是什么在C#中引用类型和价值类型之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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