Search Unity

C# Array.Push()

Discussion in 'Scripting' started by neubern, May 2, 2011.

Thread Status:
Not open for further replies.
  1. neubern

    neubern

    Joined:
    May 29, 2009
    Posts:
    18
    Okay, i know the question is stupid, but i can´t seem to use Push() in C#

    Code (csharp):
    1.  
    2. public string[] AnimationNames;
    3.  
    4. void Start () {
    5.         if (this.animation) {
    6.             AnimationNames = new string[animation.GetClipCount()];
    7.             foreach (AnimationState clip in this.animation) {
    8.                 AnimationNames.Push(clip.name);
    9.             }
    10.         }
    11.     }
    12.  
    I get this error ->

    error CS1061: Type `string[]' does not contain a definition for `Push' and no extension method `Push' of type `string[]' could be found (are you missing a using directive or an assembly reference?)
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    There is no Push for real arrays, only for fake arrays known as Array class which you normally want to avoid.

    If you need dynamic sizes, you want to use List<>, if you have primarily fixed size, you want to use [] and handle the resize manually through dublicate growing and if you don't care about performance you would use ArrayList where push or something alike should be present
     
    randyskripac likes this.
  3. by0log1c

    by0log1c

    Joined:
    Jan 30, 2011
    Posts:
    40
    As Dreamora explained, it is not possible to Push() into a built-in array, unlike with JScript array. What you can do, is use a function to do it for you, a little like this:

    Code (csharp):
    1. var myStuff:String[] = new String[9];
    2. function AddElement(element:String){
    3.     var buffer:String[] = myStuff;
    4.     myStuff = new String[buffer.length+1];
    5.     for(i=0;i<buffer.length;i++){ myStuff[i] = buffer[i]; }
    6.     myStuff[myStuff.length-1] = element;
    7. }
    I use that quite often.
     
    Last edited: May 2, 2011
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Thats very inefficient what you do there byologic.

    1. always shrink and grow arrays by *2 and /2 unless you are sure to use it very rarely only
    2. Why not myStuff.CopyTo(buffer,0) ;)
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If you're resizing arrays a lot, that generally means you should be using List anyway.

    --Eric
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Not needfully and by following 1 above, you still get a very high performance while you get the full speed at indexed ([x]) accessing.
     
  7. Jordii

    Jordii

    Joined:
    Sep 14, 2010
    Posts:
    135
    Slightly offtopic, but is it possible to use Array's Clear() for real arrays?
    I currently constantly reinitialize it with foo = new int[10]; but Clear() seems to be faster from what I've read on C# forums. However, the class Array is Javascript only, and therefore is its static function Clear() not available. Is new int[10] for me still the fastest way?
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I'm not following that...Array.Clear is for "real" (built-in, .NET) arrays only, nothing to do with Javascript arrays. It would be more efficient to clear existing entries than allocate a new array.

    --Eric
     
  9. Jordii

    Jordii

    Joined:
    Sep 14, 2010
    Posts:
    135
  10. andorov

    andorov

    Joined:
    Feb 10, 2011
    Posts:
    1,061
    I think theres a bit of a terminology issue here.

    'Real' arrays, as in classical programming are FIXED LENGTH allocations of memory.

    A .NET List<T> / JS Array is a DYNAMIC LENGTH list of variables.

    It *appears* you need a List<T>. It is available in System.Collections.Generic. Note that List<T> DOES have a "push." Its called List<T>.Insert() ;)
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I think you're confused. Built-in arrays have nothing to do with C#, they are .NET (language-independent), and you already linked to the MSDN page on using Array.Clear.

    Code (csharp):
    1. var someArray = new int[100];
    2. for (item in someArray) item = Random.Range(0, 100);
    3. System.Array.Clear(someArray, 0, someArray.Length);
    --Eric
     
  12. by0log1c

    by0log1c

    Joined:
    Jan 30, 2011
    Posts:
    40
    Hey, I didn't know about the CopyTo(). Quite useful...:)

    Could I ask for a little precision here? Why is that?
     
  13. Jordii

    Jordii

    Joined:
    Sep 14, 2010
    Posts:
    135
    Hmm damn, I switched to C# not too long ago, I always thought .NET was the toolchain Microsoft created and C# was a language. Didn't knew .NET was actually a basic layer (term feels ambiguous, like Flash).
    Good to know :)
    Was confused along with Unity's documentation.
     
Thread Status:
Not open for further replies.