本文介绍了从对象获取总字节数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有一个List< string>填充数据。我想知道它包含多少总字节数。

但这显示异常输入字符串的格式不正确。

Hi there,

I have a List<string> populated with data. And I want to know how many total bytes it contains.
But this shows an exception "Input string is in incorrect format".

List<string> lll = new List<string>();
            lll.Add("Hello");
            lll.Add("how are you");
            byte sssss = Convert.ToByte(lll);





我不想在Byte []中制作它。有什么方法可以得到这个吗?











谢谢:)



I don''t want to make it in Byte[]. is there any way i can get this?





Thanks :)

推荐答案

int count = lll.Count;

但要工作在它包含的所有字符串中有多少字节是ar,需要多做一些工作。

有很多方法可以做到,最简单的是一个显式循环:

But to work out how many bytes are ar in all the strings it contains, takes a little more work.
There are a number of ways to do it, the simplest is an explicit loop:

int count = 0;
foreach (string s in lll)
    {
    count += s.Length;
    }
Console.WriteLine(count);



或者你可以使用Linq方法:


Or you could use Linq methods:

int count = lll.Sum(s => s.Length);
Console.WriteLine(count);

但这实际上只是隐藏循环!

But that is really just "hiding" the loop!


这篇关于从对象获取总字节数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 12:32
查看更多