Create managed .NET array without initializing to all zeros -


After the

take the following C # method:

  static double [] left AddArrays (double [] Given, double [] right) {if (left! Long! = Right langent) {new argument expansion ("array is not equal length"); } Double [] results = new double [left.Length]; For (int i = 0; i & lt; left.Length; i ++) {Results [i] = left [i] + right [i]; } Return results; }  

As I understand this, CLR will start the result to all zero , even if addArrays about bus It's completely in to start it anyway. Is there any way to avoid this extra work? ? It uses unsecured C #, C ++ / CLI, or raw IL code even if

edit:. No, for the reasons mentioned,

you should do this instead:

< Pre> Fixed IEnumerable & LT; Double & gt; Adding (IEnumerable & lt; double & gt; left, ienumerable & lt; double & gt; right) using (ienumerator & lt; double & gt; l = left.GetEnumerator ()) (using IEnumerator & LT; Double> gt = R = right.GetEnumerator ()) {while (l.MoveNext () & amp; amp; amp; amp; R.MoveExt ()) {yield returns L. Current + r } If (l.MoveNext ()) r.MoveNext ()) Exceptions throwing the new argument ("the numbers of additions are not equal length"); }}

You can pass your double arrays on this function. If you really need an array as a result (hint: you probably do not) then you can just call .oAray () on the return value of the function.

Net 4 will already have a function created for this:

  double [] array1 = {1.0, 2.0, 3.0}; Double [] array 2 = {4.0, 5.0, 6.0}; IEnumerable & LT; Double & gt; Result = array 1. Zip (array 2, (a, b) => a + b); Foreach (result in Double D) {console. WrightLine (D); }  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -