惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
G
Google Developers Blog
罗磊的独立博客
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
A
About on SuperTechFans
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
The Register - Security
The Register - Security
U
Unit 42
D
Docker
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Google DeepMind News
Google DeepMind News

博客园 - Bobby

SQL Top注意事项 asp.net回车提交表单 - Bobby - 博客园 Asp.Net XML操作基类(修改,删除,新增,创建) 项目中的代码 客户端全部选中或取消Repeater中的CheckBox - Bobby - 博客园 javascript技巧参考(转载) js倒计时关闭窗口&计算字数 - Bobby - 博客园 CSS控制GridView固定表头 - Bobby - 博客园 经典算法-C#四种排序算法 js设置调用cookie设置背景颜色 字号、颜色 js简繁转换代码 ASP.NET错误处理的方式(总结) ASP.NET错误处理和程序优化 学习SQL应知道的动态SQL语句基本语法 js宝典学习笔记【转载】 在b/s开发中经常用到的javaScript技术整理 Visual C#常用函数和方法集汇总 SQL SERVER 与ACCESS、EXCEL的数据转换 - Bobby 不被浏览器屏蔽的对联广告
C#关于值类型和引用类型
Bobby · 2007-09-04 · via 博客园 - Bobby

数据在内存中的存储位置,取决于它的数据类型,在C#中,分为值类型和引用类型,值类型的数据存储在内存中的堆栈中,每个变量或程序都有自己的堆栈,不可以共用一个堆栈地址。当数据一个值类型的变量传递到另一个相同类型的变量时,会在堆栈中分配两个不同的地址。

而引用类型的数据存储在内存中的堆中,可以不同的变量或程序共同使用同一个位置的数据。当数据从一个引用类型的变量传递到另一个相同类型的变量时,只是把这个变量的引用地址传递给新的变量,同时引用当前堆中存储的数据。

可以通过实例得到详细结论:

using System;
using System.Collections.Generic;
using System.Text;namespace RefRectangle
{
    
class RefRectangle
    {
        
//定义一个矩形类 属于引用类型
        public int width;
        
public int height;
    }
//定义一个矩形结构 属于值类型
    struct ValRectangle
    {
        
public int width;
        
public int height;
    }
class RefValRectangle
    {
        
public static void Main()
        {
            
//创建矩形对象  并将值传递给另一个新对象
            RefRectangle ref1 = new RefRectangle();
            ref1.width 
= 3;
            ref1.height 
= 4;

            RefRectangle ref3 

= ref1;

            Console.WriteLine(

"Dimensions of ref are:" + ref3.width.ToString() + "" +ref3.height.ToString());
            Console.WriteLine(
"change dimensions of ref1");
            ref1.width 
= 10;
            ref1.height 
= 50;bool bTransfer = ref3.Equals(ref1);

            Console.WriteLine(

"Dimensions of ref1 now are :"+ ref3.width.ToString() +"." +ref3.width.ToString());

            Console.WriteLine(bTransfer.ToString());

            Console.ReadLine();

//创建一个新的矩形结构,将值传递给一个新的矩形结构

            ValRectangle val1 
= new ValRectangle();
            val1.width 
= 3;
            val1.height 
= 4;

            ValRectangle val3 

= val1;

            Console.WriteLine(

"Dimensions of val are:" + val3.width.ToString() +""+ val3.height.ToString());
            Console.WriteLine(
"Change  Dimensions of val1");

            val1.height 

= 10;
            val1.height 
= 50 ;bool bPass = val3.Equals(val1);
            Console.WriteLine(
"Dimensions of val1 now are : " + val3.width.ToString() + ".." + val3.height.ToString());
            Console.WriteLine(bPass.ToString());
            Console.ReadLine(); 
        }
    }
}

可以看到,当值类型的变量传递后,改变第一个变量,不会影响第二个变量的值,这是因为,当变量传递时,是在堆栈中又分配了一个地址给新的变量,所以这个两个变量在传递发生后,不再有关系。
而引用类型的变量传递后,改变第一个,第二个变量随之改变,是因为两个变量同时引用堆中的一个地址的内容,当一个变量改变,对应与内存中的堆也随之改变,而另外的一个变量也随之改变。