值类型和引用类型 [英] Value Type and reference type

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

问题描述

我在许多网站上搜索了解决方案,但对这些答案我不满意.

请澄清我的疑问.


为什么整数是值类型,为什么字符串是引用类型.

从理论上我知道,int存储在堆栈中,而字符串存储在堆中.但是我不清楚.

例如:

I Searched in many websites for solution,but i am not satisfied with that answers.

Please clarify my doubt.


Why integer is valutype and why string is Reference type.

I know theoretically,int is stored in stack and string is store in heap.but i did not have clarity.

For example:

int i=10;
int j=i;
i=12;
console.writeline(i);
console.writeline(j);




O/P ;
i = 12; j = 10;

类似地




O/P;
i=12;j=10;

similarly

string str1="Hello";
string str2=str1;
 str1="World";
            System.Console.WriteLine(str1);
            System.Console.WriteLine(str2);



O/p:



O/p:

str1=World;
str2=Hello;



整数和字符串都正确...



Both integer and string are same right...

class AddPrg
   {
       public String Name
       {
           get;
           set;
       }

   }


class CPrograms
   {


       static void Main()
       {

 AddPrg obg = new AddPrg();
             obg.Name = "Hello";
             AddPrg obg1 = obg;
             obg1.Name = "World";
             System.Console.WriteLine(obg1.Name);
             System.Console.WriteLine(obg.Name);


}}



O/P是World,World都相同.所以该类是引用类型.



希望您理解我的问题.请澄清我的疑问..提前致谢...



O/P is World,World both are same.so that class is reference type.



Hope you understand my question.Please clarify my doubt.. Thanks in Advance...

推荐答案

我通过引用JAVA给出此答案:

整数是基本数据类型,而字符串不是.

int被视为单个组件.

而string是类名String
的对象
当你写
int a;
a指向存储int编号的内存位置
当你写
字符串a;
一个不是字符串的对象.对包含原始数据的字符串对象的引用的引用

java a中的类型为I m,通过引用JAVA给出此答案:

整数是基本数据类型,而字符串不是.

int被视为单个组件.

而string是类名String
的对象

当你写
int a;
a指向存储int编号的内存位置
当你写
字符串a;
一个不是字符串的对象.对包含原始数据的字符串对象的引用的引用

在JAVA中,a的类型类型不是String.a指向类型为String
的对象
选中此示例,更新a也会更新b,这可能是引用类型而不是值类型的情况.

I m giving this answer by reference to JAVA:

integer is basic datatype while string is not.

int is treated like single component.

While string is an object of a class name String

when u write
int a;
a refer to the memory location where int number is stored
while when u write
String a;
a is not string object. a fefer to an reference of string object which contain original data

in java a is of type I m giving this answer by reference to JAVA:

integer is basic datatype while string is not.

int is treated like single component.

While string is an object of a class name String


when u write
int a;
a refer to the memory location where int number is stored
while when u write
String a;
a is not string object. a fefer to an reference of string object which contain original data

in JAVA a will be of class type not String.a will point to object of type String

Check this exapmle updating a will also update b which is possible in reference type not in valutype.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            myString a=new myString();
            myString b = a;
            a.Name = "Hello";
            Console.Write(a.Name);
            Console.Write(b.Name);

            a.change();

            Console.Write(a.Name);
            Console.Write(b.Name);
        }
    }
    public class myString
    {
        public String Name
        {
            get;
            set;
        }
        public void change()
        {
            Name="World";
        }

    }
}


这不是那么简单.首先,可以将值类型存储在1)堆栈中; 2)在为静态数据保留的数据段中,3)在堆上,这是引用类型的成员.

至于引用类型,它也不是那么简单.引用类似于指针(但由于托管(基于垃圾收集器)体系结构而非常不同).始终涉及两个内存对象:引用本身和引用的对象.引用的对象只能存储在堆中,而引用本身可以存储在任何地方,就像值对象一样.

在C#中,您不能单独或显式访问引用和引用对象,但这在C ++/CLI中是可能的.

您也不要忘记拳击,请参阅 http://msdn.microsoft.com/zh-CN/library /yz2be5wk.aspx [ ^ ].

现在,字符串类非常特殊.这是一种完全模拟值语义的引用类型.其功能基于字符串池实习生,请参见:
http://en.wikipedia.org/wiki/String_interning [ http://java.codeproject.com/Answers/153650/String-a-reference-type-variable-creating-confussi#answer2 [字符串是值类型还是引用类型. [ ^ ],
C#中的可变字符串和不可变字符串之间的区别 [ ^ ].

—SA
This is not so simple. First of all, value type can be stored 1) in stack; 2) in data segment reserved for static data, 3) on heap is this is a member of reference type.

As to reference types, it is also not so simple. References are something like pointers (but very different, due to managed (Garbage Collector based) architecture). There are always two memory objects involved: a reference itself and the referenced object. The referenced object can be stored only in heap, but the reference itself — anywhere, exactly like a value object.

In C#, you cannot access reference and reference object separately or explicitly, but this is possible in C++/CLI.

You should not also forget about boxing, see http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx[^].

Now, string class is very special. This is a reference type thoroughly simulating value semantic. Its functionality is bases on string pool and interning, pleas see:
http://en.wikipedia.org/wiki/String_interning[^]

For further detail, please see my past answers:
http://java.codeproject.com/Answers/153650/String-a-referance-type-variable-creating-confussi#answer2[^],
Is string a value type or Reference type..[^],
Difference Between Mutable , Immutable string in C#[^].

—SA


Int是一种值类型,因为它是一种非常简单的数据类型:固定长度,小巧,易于处理的操作.
另一方面,字符串要复杂得多.它具有可变长度(最大2 Gb!),且操作复杂.它也是不可变的,对于基于堆栈的数据不可行.

在堆栈上放一个字符串是不合理的:单独的大小可能使它把堆栈炸成碎片!
Int is a value type because it is a very simple type of data: fixed length, small, with easy to handle operations.
String on the other hand is a lot more complex. It is of variable length (up to 2 Gb!) with complicated operations. It is also immutable, which is not practical with stack based data.

It would not be reasonable to put a string on the stack: the size alone could make it blow the stack to pieces!


这篇关于值类型和引用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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