re-initialize array [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I re-initialize an array?
public int[] numbers;
In my case numbers will be set with an unknown-length array. For example, when I use it first, it will have 8 elements, next time only 5, then 12.
I read some about this topic, and found only one relevant:
http://social.msdn.microsoft.com/Forums/en-US/bee99ac8-4ade-40ac-aa78-8d14d1d7bf9f/c-reinitialize-arrays?forum=csharplanguage
Even if I do not care about the memory usage of redeclaration (uhh), I can't make it this way, since I'm going to use the array in a timer's thick method, and gonna use the same array, until the timer stops. Then call the function again, which would put the elements into the array, then start the timer again.
Edit:
There are 3 functions in my case and the array declare as a public variable. The first function is called doAction(), which calls another function which returns an array with unknown length. Then, doAction starts the timer1 (thick method), which needs to reach the array from doAction. That's the reason why I used a global variable to put the array in.

Of course you can do it that way. You cannot change the size of an array. When you stop the Timer, simply create a new array and assign it to the same variable. It's the variable that matters. You'll be getting the array from the variable in the Tick event handler so you'll always get the current array.

When you use it first, do
numbers = new int[5];
when you use it again, do
numbers = new int[12];
If it's not that simple, then you haven't explained your problem very well.

Related

In c#, how can I manually move the memory that a reference type points to, into a pinned, fixed, contiguous array or buffer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I need a buffer of memory which holds actual objects, not references, in memory.
I am using Unity Engine for my app. In order to submit a draw-call in Unity, one must provide a Mesh-object and Material-object in the function call. Here
is the part of my algorithm I am trying to optimize.
Mesh[] meshes;
Material[] materials;
Foreach (mesh-type)
DrawMeshInstancedIndirect(mesh[i],material[i]...etc);
I want to be able to iterate over a buffer of memory directly containing the object data, with no references, because I don't want to fill my cpu cache with useless data fetching each object in random parts of memory. You cannot substitute structs for these special Unity classes.
It doesn't matter if it is bad practice or a very messy solution. This is a performance-critical part of my app where structs cannot be substitute for classes.
Here is a reference to the draw-call function, for more context.
https://docs.unity3d.com/ScriptReference/Graphics.DrawMeshInstancedIndirect.html
Try the fixed keyword.
class Foo
{
public int[] stuff = new int[999];
public unsafe void ExamplePinned()
{
fixed (int* pStuff = stuff)
{
//Alright, the data array is all set and ready to go. Think of 'pStuff' as a pointer
//to the first element in the array. You can use it to access the memory of the
//array and copy it to another buffer
}
}
}

How do I take an indefinitely large set of user inputs and save them into an array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am making a program (in school for intro to computer programming(dont worry, this is all extra credit stuff, not asking for cheat answers)) that will take a user decided set of user inputs and save them all to do math (more specifically, finding the median, but I want to figure that out myself) on them. I am fairly sure that I need an array to do this (even my teacher hinted that that is what you needed to do).
My plan is to have a variable x that will decide the amount of separate numbers in the array (will not only be the number in the array, but also the number to check how many times I want to run the loop for asking for numbers), and then to have that many user inputted numbers inputting by the user, and then to be able to take those numbers and find the median of them (I will probably have to check if the number is even or odd first, then sort the numbers (somehow), then find the middlemost number.)
Thanks!
Instead of using array which has fixed size, try using List.
List allows you to add indefinitely many (until it fits into memory) elements.
So, in your for loop you could read user input and add it to the list.
After that you can sort entire list using sort method and return middle element, which is a median.
It could look like that:
int n = //TODO: read number of user inputs
List<int> elems = new List<int> ();
for (int i = 0; i < n; i++) {
int x = // get input
elems.Add(x);
}
Console.WriteLine(elems[elems.Length / 2]);
You can take a look at the List<T> class here for storing the numbers.
For reading input, see Console.ReadLine.
For converting string to int, take a look at int.TryParse.
I gave you the tools, now start coding! :D

Independent Variables and Array performance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I would like to know what is more optimal, A number N of private integer variables, or a single array containing N integer values.
When you use an array, that is a plus indirection. The array will be allocated at a separate part of the memory, so when you first access it, your code first obtains its address, then it is able to read out its content. It also needs some indexing, but that is done extremly fast by the CPU. However, .NET is a safe environment and it will do a check whether you use a valid array index. It adds additional time.
When you use separate variables, these will be encompassed by your object instance and no indirection is needed. Also, no index bound check is needed.
Moreover, you cannot name nicely the Nth element of an array, but you can give good names for individual variables. So your code will be readable.
As others mentioned, you shouldn't do this kind of optimalizations, the compiler/jitter take care of it. The compiler knows several common use cases and has optimialization strategy for that. If you start doing tricky things, the compiler will not recognize your intention and cannot make the optimalization for you.

Adding a new element to the end of a C# array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Im trying to do exactly what the title says
int[] weeks = {};
weeks[weeks.Length]=1;
this doesnt work. There is also no .Add Method.
Any ideas? or is this not possible
Line 1 declares and initializes a new 1-dimensional array of int without specific size.
Line 2 resizes the array (weeks) to its actual size, plus 1.
Line 3 assigns the value 1 to the element at the last position (that we created in Line 2)
Remember: int[5] weeks; -> to access last element you have to use index 4 -> weeks[4] = 1
int[] weeks = {};
Array.Resize(ref weeks,weeks.Length + 1);
weeks[weeks.Length - 1]=1;
Arrays in C# by definition are of a fixed size, and cannot be dynamically expanded.
I would suggest using a List<>, as they can be dynamically expanded, much like vectors in other programming languages.
List<int> weeks = new List<int>();
weeks.add(1);
See more here.
First of all there is no such thing razor array. Razor is a view engine and array is a data structure.
Arrays have fixed length so if you declare an array with the length of 5:
int[] weeks = new int[5];
Trying to add an element to a fifth place will result in IndexOutOfRangeException
If you need some data-structure with variable size you could look at all the objects that implement IList interface for example a List, an ArrayList and others.
IList interface also defines Add method that you requested.

why is Next() method called next? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Why does Random.Next() have Next in it's name? I know what it does, but the name doesn't seem to correspond to it.
It returns the next number in the infinite sequence of numbers generated from your Random instance's seed.
In computer science jargon, a "generator" is a specific kind of function: one that returns a different result each time it is called. It is traditional to call this function something like next(), because they are often used to return the next piece of a sequence (perhaps infinite). RNGs are just a special case of generator function, returning the next value in a calculated sequence.

Categories