C#参考类型分配VS值类型分配 [英] C# Reference type assignment VS value type assignment

查看:70
本文介绍了C#参考类型分配VS值类型分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,分配一个引用类型变量到另一个,只能引用被复制,而不是对象的理论概念。分配一个值类型变量到另一个时,对象被复制。但我不能当场代码不同。会有人善意指出了下面的两个代码块之间的区别? !谢谢



REFERENCE类型分配

 使用系统; 

类员工
{
私人字符串m_name;

公共字符串名称
{
{返回m_name; }
集合{m_name =价值; }
}
}

类节目
{
静态无效的主要()
{
员工乔=新员工( );
joe.Name =乔;

员工鲍勃=新员工();
bob.Name =鲍勃;

Console.WriteLine(原员工价值观:);
Console.WriteLine(乔=+ joe.Name);
Console.WriteLine(鲍勃=+ bob.Name);

//分配乔参考鲍勃变量
BOB =乔;

Console.WriteLine();
Console.WriteLine(参​​考值分配后:);
Console.WriteLine(乔=+ joe.Name);
Console.WriteLine(鲍勃=+ bob.Name);

joe.Name =波比乔;

Console.WriteLine();
Console.WriteLine(值更改一个实例之后:);
Console.WriteLine(乔=+ joe.Name);
Console.WriteLine(鲍勃=+ bob.Name);

Console.ReadKey();
}
}



VALUE类型分配

 使用系统; 

结构高度
{
私人诠释m_inches;

公众诠释英寸
{
{返回m_inches; }
集合{m_inches =价值; }
}
}

类节目
{
静态无效的主要()
{
高度乔=新的高度( );
joe.Inches = 71;

高度鲍勃=新的高度();
bob.Inches = 59;

Console.WriteLine(原高
值:);
Console.WriteLine(乔=+ joe.Inches);
Console.WriteLine(鲍勃=+ bob.Inches);

//分配乔参考鲍勃变量
BOB =乔;

Console.WriteLine();
Console.WriteLine(值值分配后:);
Console.WriteLine(乔=+ joe.Inches);
Console.WriteLine(鲍勃=+ bob.Inches);

joe.Inches = 65;

Console.WriteLine();
Console.WriteLine(值更改一个实例之后:);
Console.WriteLine(乔=+ joe.Inches);
Console.WriteLine(鲍勃=+ bob.Inches);

Console.ReadKey();
}
}


解决方案

好中,明显的区别在于,与类例子,它显示Joe和在最后部分鲍勃改变那里,为相同的值。



在该结构示例中,它们保持自己独立的价值,只是因为每个变量本身就是一个整体结构的价值,而不仅仅是一个在内存的某个地方共同的对象引用。



的主要代码明智的区别是您使用类型,结构的,这决定了不管你是创建一个引用类型或值类型。


I understand the theoretical concept that assigning one reference type variable to another, only the reference is copied, not the object. assigning one value type variable to another, the object is copied. But I cannot spot the different in the code. would someone kindly point out the difference between the following two code blocks? Thank you!

REFERENCE TYPE ASSIGNMENT

using System;

class Employee
{
    private string m_name;

    public string Name
    {
        get { return m_name; }
        set { m_name = value; }
    }
}

class Program
{
    static void Main()
    {
        Employee joe = new Employee();
        joe.Name = "Joe";

        Employee bob = new Employee();
        bob.Name = "Bob";

        Console.WriteLine("Original Employee Values:");
        Console.WriteLine("joe = " + joe.Name);
        Console.WriteLine("bob = " + bob.Name);

        // assign joe reference to bob variable
        bob = joe;

        Console.WriteLine();
        Console.WriteLine("Values After Reference Assignment:");
        Console.WriteLine("joe = " + joe.Name);
        Console.WriteLine("bob = " + bob.Name);

        joe.Name = "Bobbi Jo";

        Console.WriteLine();
        Console.WriteLine("Values After Changing One Instance:");
        Console.WriteLine("joe = " + joe.Name);
        Console.WriteLine("bob = " + bob.Name);

        Console.ReadKey();
    }
}

VALUE TYPE ASSIGNMENT

using System;

struct Height
{
    private int m_inches;

    public int Inches
    {
        get { return m_inches; }
        set { m_inches = value; }
    }
}

class Program
{
    static void Main()
    {
        Height joe = new Height();
        joe.Inches = 71;

        Height bob = new Height();
        bob.Inches = 59;

        Console.WriteLine("Original Height
            Values:");
        Console.WriteLine("joe = " + joe.Inches);
        Console.WriteLine("bob = " + bob.Inches);

        // assign joe reference to bob variable
        bob = joe;

        Console.WriteLine();
        Console.WriteLine("Values After Value Assignment:");
        Console.WriteLine("joe = " + joe.Inches);
        Console.WriteLine("bob = " + bob.Inches);

        joe.Inches = 65;

        Console.WriteLine();
        Console.WriteLine("Values After Changing One Instance:");
        Console.WriteLine("joe = " + joe.Inches);
        Console.WriteLine("bob = " + bob.Inches);

        Console.ReadKey();
    }
}

解决方案

Well, the obvious difference is that with the class example, it appears both joe and bob changed in the last part there, to the same value.

In the struct example, they keep their separate values, simply because each variable is a whole struct value by itself, not just a reference to a common object in memory somewhere.

The main code-wise difference being the type you use, class or struct, this dictates whether you're creating a reference type or a value type.

这篇关于C#参考类型分配VS值类型分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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