C# 可以创建动态 StringBuilder 数组吗?

发布于 2024-11-28 04:05:24 字数 667 浏览 1 评论 0原文

我正在尝试在 C# 中动态创建 StringBuilder 数组,但运气不佳。

我想根据给定的客户端数字向 StringBuilder (sb) 添加信息,该数字将根据我可能拥有的客户端数量动态变化。我可能有 2 个,也可能有 20 个。稍后在我的程序中,该 sb 将用于构建基于每个客户的报告。

例如,我有以下 3 个客户:

    Client A = 0 
    Client B = 1
    Client C = 2

如果客户 A 报告他有 8 个苹果,我想将字符串“8 apples”添加到 sb[0]。

如果客户 B 报告他有 3 个橙子,我想将字符串“3 Oranges”添加到 sb[1]。

以上只是我想要实现的想法的一个简单示例。事实上,我会向某人添加很多信息。

我尝试了以下方法,但没有让它们按照我预期或想要的方式工作。

StringBuilder[] sbFileError = new StringBuilder[clientCount];
List<StringBuilder> sbFileError = new List<StringBuilder>();

有什么想法吗?是否可以创建 StringBuilder 数组?

谢谢!

I am trying to dynamically create a StringBuilder array in C# and I haven't had much luck.

I want to add information to a StringBuilder (sb) based off of a given client digit that will change dynamically depending on how many clients I might have. I might have 2 or I might have 20. The sb will be used to build a report based per client later in my program.

For example I have the following 3 clients:

    Client A = 0 
    Client B = 1
    Client C = 2

If Client A is reporting he has 8 apples I want to add the string "8 apples" to sb[0].

If Client B is reporting he has 3 oranges I want to add the string "3 oranges" to sb[1].

The above is just a simple example of the idea I am trying to accomplish. In reality I am going to be adding a lot of information to the sb.

I have tried the following without much luck getting them to work they way I expected or wanted.

StringBuilder[] sbFileError = new StringBuilder[clientCount];
List<StringBuilder> sbFileError = new List<StringBuilder>();

Any thoughts? Is it possible to create a StringBuilder array?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

何止钟意 2024-12-05 04:05:24

您已经创建了上面的容器,但您需要在其中填充一些东西。例如:

StringBuilder[] sbFileError = new StringBuilder[clientCount];
for (int ix = 0;  ix < clientCount;  ++ix)
    sbFileError[ix] = new StringBuilder();

或者

List<StringBuilder> sbFileError = new List<StringBuilder>();
for (int ix = 0;  ix < clientCount;  ++ix)
    sbFileError.Add(new StringBuilder());

You've created the containers above, but you need to fill them with something. For example:

StringBuilder[] sbFileError = new StringBuilder[clientCount];
for (int ix = 0;  ix < clientCount;  ++ix)
    sbFileError[ix] = new StringBuilder();

Or

List<StringBuilder> sbFileError = new List<StringBuilder>();
for (int ix = 0;  ix < clientCount;  ++ix)
    sbFileError.Add(new StringBuilder());
夏见 2024-12-05 04:05:24

创建数组是第一步,但这只会创建存储 StringBuilder 的位置。此时您实际上还没有实例化任何 StringBuilder。你需要做这样的事情......

StringBuilder[] sbFileError = new StringBuilder[clientCount];
for (int i = 0; i < sbFileError.Length; i++)
    sbFileError[i] = new StringBuilder();

Creating the array is the first step, but this only creates places to store StringBuilders. You haven't actually instantiated any StringBuilders at this point. You'll need to do something like this....

StringBuilder[] sbFileError = new StringBuilder[clientCount];
for (int i = 0; i < sbFileError.Length; i++)
    sbFileError[i] = new StringBuilder();
绝情姑娘 2024-12-05 04:05:24

我认为您缺少数组元素的实例化。
这段代码有效。

int clientCount = 3;
StringBuilder[] sbFileError = new StringBuilder[clientCount];
for(int i=0; i<clientCount; i++)
{
    sbFileError[i] = new StringBuilder();
}

sbFileError[1].Append("Hello World!");

I think you are missing instantiation of array elements.
This code works.

int clientCount = 3;
StringBuilder[] sbFileError = new StringBuilder[clientCount];
for(int i=0; i<clientCount; i++)
{
    sbFileError[i] = new StringBuilder();
}

sbFileError[1].Append("Hello World!");
疯了 2024-12-05 04:05:24
List<StringBuilder> sbFileError = new List<StringBuilder>();

看起来不错,但你必须填写它:

for (int i = 0; i < numClients; i++)
   sbFileError.Add(new StringBuilder());
List<StringBuilder> sbFileError = new List<StringBuilder>();

Looks OK, but you have to fill it:

for (int i = 0; i < numClients; i++)
   sbFileError.Add(new StringBuilder());
左耳近心 2024-12-05 04:05:24

你也可以想像一下:

var items = Enumerable.Range(0, clientCount)
                      .Select(i => new StringBuilder());

var list = new List<StringBuilder>(items);

或将其缩短为:

var list = Enumerable.Range(0, clientCount)
                     .Select(i => new StringBuilder())
                     .ToList();

只是篮子里的另一个鸡蛋。

You could also get fancy:

var items = Enumerable.Range(0, clientCount)
                      .Select(i => new StringBuilder());

var list = new List<StringBuilder>(items);

or shorten it to:

var list = Enumerable.Range(0, clientCount)
                     .Select(i => new StringBuilder())
                     .ToList();

Just another egg in the basket.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文