Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

String Array in C#

Discussion in 'Scripting' started by _Adriaan, Feb 16, 2010.

  1. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    481
    I am totally missing the point here. How do I make something simple as a string array (or something like it?) in C#?

    Whoever shows me the easy code first receives a cookie!
     
  2. fallingbrickwork

    fallingbrickwork

    Joined:
    Mar 16, 2009
    Posts:
    1,072
    int[] numbers = new int[] {1, 2, 3, 4, 5};
    int[] numbers = new int[5];
    string[] names = new string[] {"Matt", "Joanne", "Robert"};

    You can also omit the new operator if an initializer is provided, like this:

    int[] numbers = {1, 2, 3, 4, 5};
    string[] names = {"Matt", "Joanne", "Robert"};

    Look at the Collections classes too as they have more power and are generally nice (IMO)
     
  3. NagarajVasu

    NagarajVasu

    Joined:
    Jun 5, 2013
    Posts:
    26
    Awasome dude...You list out the whole string arrays

    thanks,
     
  4. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    Or use lists (List<string> myList = new List<string>()) to have some more dynamic options on the fly. (using System.Collections.Generic).
     
    Thor20 and damobe like this.
  5. Jacksendary

    Jacksendary

    Joined:
    Jan 31, 2012
    Posts:
    408
    Just to let you know, there is something called list where you can dynamically add and remove objects. List works very well in cases where you do not know how many objects you'll need instead of having a huge array since arrays are "static" and cant get their's size change other than copying the whole array which is a huge performance lost! - Good practice (from what i learned from OOP); Only use arrays if you always have a static size that and you use all the spaces, otherwise keep to list.
     
  6. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    The problem with list is the order of elements will constantly change. Or the size will change. In most cases, using a standard container is the best way to go.
     
  7. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    What? The order of elements will change? Never seen that happening! Why would they do that? (It may be you are right, it's a sincere question: When would they do that?)
     
  8. XGundam05

    XGundam05

    Joined:
    Mar 29, 2012
    Posts:
    473
    Unless you are implementing your own List<> system, this doesn't happen. Ever. The reason for this is that List<T> is built on top of a standard array.

    The size only changes when you add or remove items to/from the List<T> using .Add() or .Remove(). The Count property always returns the number of existing items in the List<T>. When adding to the List, it checks to see if the current underlying array has a sufficient size to hold the new item, if not, it creates a new, larger array and copies the items to it. Then it adds the item to the new array.

    More often than not, the functionality of List<T> is what you'll need anyways. And if you initialize the List correctly, it performs the same as a standard array as far as access times go (O(1) for accessing an element via index).
     
    Last edited: Jun 28, 2013