什么是memset的在C#中的相同呢? [英] What is the equivalent of memset in C#?

查看:185
本文介绍了什么是memset的在C#中的相同呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要填写字节[] 用一个非零值。我怎么能做到这一点在C#中,无需通过每个字节数组中的循环?

I need to fill a byte[] with a single non-zero value. How can I do this in C# without looping through each byte in the array?

更新:的意见似乎这个分裂成两个问题 -

Update: The comments seem to have split this into two questions -


  1. 是否有一个框架的方法,以填补一个byte [],可能是类似于 memset的

  2. 什么是最有效的方式做到这一点,当我们正在处理一个非常大阵?

我完全同意,使用一个简单的循环工作得很好,埃里克和其他人指出。问题的关键是要看看我能学到新的东西关于C#的:)我认为朱丽叶的每个并行操作方法应该是速度甚至超过了一个简单的循环。

I totally agree that using a simple loop works just fine, as Eric and others have pointed out. The point of the question was to see if I could learn something new about C# :) I think Juliet's method for a Parallel operation should be even faster than a simple loop.

测试:
多亏了史云逊的Mikael:<一href=\"http://techmikael.blogspot.com/2009/12/filling-array-with-default-value.html\">http://techmikael.blogspot.com/2009/12/filling-array-with-default-value.html

原来简单的循环去,除非你想使用不安全code的方式。

It turns out the simple for loop is the way to go unless you want to use unsafe code.

对于不是在我原来的职位更明确的道歉。埃里克和马克都在他们的意见都正确;需要有肯定更集中的问题。感谢大家的意见和反应。

Apologies for not being clearer in my original post. Eric and Mark are both correct in their comments; need to have more focused questions for sure. Thanks for everyone's suggestions and responses.

推荐答案

您可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/bb348899.aspx\"><$c$c>Enumerable.Repeat:

byte[] a = Enumerable.Repeat((byte)10, 100).ToArray();

第一个参数是你想重复元素,第二个参数是的次数重复。

The first parameter is the element you want repeated, and the second parameter is the number of times to repeat it.

这是小数组确定,但如果你是非常大的阵列和处理性能是一个问题,你应该使用循环方法。

This is OK for small arrays but you should use the looping method if you are dealing with very large arrays and performance is a concern.

这篇关于什么是memset的在C#中的相同呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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