多少空间的String.Empty和空走? [英] How much space do string.Empty and null take?

查看:171
本文介绍了多少空间的String.Empty和空走?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多少内存做一个空字符串和走?

我发现这个问题,它告诉有关的String.Empty 的内存分配,但没有

如果我想存储一个空字符串或在会议其中一个将采取更小的空间?

  MyClass类
    {
        公共字符串一个{获得;组; }
        公共字符串两个{获得;组; }

        公共MyClass的(字符串之一,字符串二)
        {
            一个=之一;
            两=两个;
        }
    }
    一流的主
    {
       VAR EMP =新MyClass的(的String.Empty,的String.Empty);
       VAR NUL =新MyClass的(NULL,NULL);
    }
 

解决方案

MyClass的,就会有绝对没有什么区别。两者都将是的基准尺寸 - 无论是4字节或8字节。它也将采取相同的空间量,如果值所指的任何其他字符串

当然,空字符串的对象的占用空间,但它占用的空间然而,许多其他的引用相同数量也有它。 (换言之,无论是的的引用与否将不会影响到内存的... 的String.Empty 字段仍然指向它,所以它不喜欢它都不能被垃圾收集。)

How much memory do an empty string and null take?

I found this question, which tells about the String.Empty memory allocation but not null.

If I want to store an empty string or null in session which one would take less space?

    class MyClass
    {
        public string One { get; set; }
        public string Two { get; set; }            

        public MyClass(string one,string two)
        {
            One = one;
            Two = two;
        }
    } 
    class Main
    {
       var emp = new MyClass(String.Empty, String.Empty);
       var nul = new MyClass(null,null);
    }

解决方案

Within MyClass, there'll be absolutely no difference. Both will be "the size of a reference" - either 4 bytes or 8 bytes. It would also take the same amount of space if the values referred to any other strings.

Of course the empty string object takes up space, but it takes up that the same amount of space however many other references there are to it. (In other words, whether you refer to it or not will make no difference to memory... the string.Empty field will still refer to it, so it's not like it can ever be garbage collected.)

这篇关于多少空间的String.Empty和空走?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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