存储可变大小的数据最安全的选择是什么? [英] what can be the safest option for storing data of variable size?

查看:47
本文介绍了存储可变大小的数据最安全的选择是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在Web应用程序中工作,在该应用程序中我必须将数据(字符串格式)保存在字符串"变量中.但是,数据大小可变.它的大小可以大于10 mb或小于100kb.目前,我将数据保存在字符串"变量中.这还没有经过测试.但是我怀疑,字符串"变量可以容纳大于10 mb的数据吗?还是应该使用StringBuilder类?请让我知道什么是最安全的选择或任何其他解决方案.

在此先感谢.
问候,
Akash

hii all,
I am working on Web application in which I have to hold data(String Format) in ''string'' variable. but, data is of variable size. Its size can be more than 10 mb or less than 100kb. Currently, I am holding data in ''string'' variable. This is not tested yet.but I doubt, can ''string'' variable hold data greater than 10 mb? OR should I use StringBuilder class ? Please, let me know what can be the safest option or any other solution.

thanks in advance.
regards,
Akash

推荐答案

Length参数是一个Int,它给出字符串或stringbuilder的最大实践大小为2,147,483,647个字符(不要忘记对int进行了签名,因此不会全部使用32位).两种格式都可以容纳这个数目的字符,但是如果必须重新分配过多的内存,您的应用程序可能会在完全填满之前耗尽内存.
请记住,字符串是不可变的-如果您添加到字符串中,则将获得原始字符的副本,其中添加了新字符,而不是原始字符的末尾附加了额外的空格.当StringBuilder空间不足时,也会发生同样的事情-它分配一个新的内存块两倍大,并将其全部复制.如果要构造真正的大字符串,请使用StringBuilder并在构造时分配足够的空间来容纳所需的所有字符.
10mb是一个很大的字符串,但是在.NET处理范围之内.
The Length parameter is an Int, which gives a maximum practicle size for a string or stringbuilder of 2,147,483,647 characters (don''t forget int is signed, so won''t use all 32 bits). Either will hold this number of characters, but your application may run out of memory before you completely fill either if it has to reallocate memory too much.
Remember that strings are immutable - if you add to a string you get a copy of the original with the new characters added rather than the original with extra space appended on the end. The same thing happens with StringBuilder when it runs out of space - it allocates a new memory block twice as big and copies it all over. If you are constructing realy big strings, then use the StringBuilder and allocate sufficient space at the construction to hold all the characters you will need.
10mb is a large string, but well within the bounds of .NET handling.


如果您使用的是.Net 3.0,则可以浏览var [
IF you are using .Net 3.0, you can explore the var[^] data type.


为什么需要一次读取10MB的整个字符串并将其存储在字符串变量中.

相反,我希望看到您使用固定缓冲区仅读取字符串的一部分.

通常,在解析文件时,我们使用缓冲区来处理这种情况,而不是一次读取文件包含的所有字节. :)
Why do you require to read the whole string of 10MB at a time and store it in a string variable.

Rather I would like to see you reading only a part of the string using a fixed buffer.

Generally while we parse a file we use buffer to handle this type of situation rather than going to read all the bytes the file contains at a time. :)


这篇关于存储可变大小的数据最安全的选择是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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