Results 1 to 3 of 3

Thread: C# array of strings

  1. #1
    Junior Member Xarzu's Avatar
    Join Date
    Jan 2008
    Posts
    15

    C# array of strings

    In a main routine of a c sharp program, you have a string array like this:
    public static int Main(string[] arguments)

    Well, let's suppose I am mimicing this sort of function. Would this be the proper use of a string array?
    string[] arguments;

    arguments[0] = "C:\\Users\\All Users\\Apple\\Some File.msi";

    arguments[1] = "C:\\Users\\All Users\\Apple\\Another FIle.msi";

    I get a compile error
    Compiler Error CS0165 right on the first line.

    I guess this is not how to assign strings in C#. But I thought it was.

  2. #2

  3. #3
    Senior Member codenamevirus's Avatar
    Join Date
    Jun 2005
    Location
    Faridabad, Haryana, India
    Posts
    298
    hi Xarzu

    Quote Originally Posted by Xarzu View Post
    string[] arguments;
    you have initialize the array before you can assign it any value. Also in c#, you need to give the size of the array.

    So, use some thing like:
    string[] arguemnts = new string[2];

    then you can use the following statements:

    arguments[0] = "C:\\Users\\All Users\\Apple\\Some File.msi";

    arguments[1] = "C:\\Users\\All Users\\Apple\\Another FIle.msi";

    However, if you dont know the size of the array before and you would like to have something which has dynamic size and you can choose to use a List<> collection.

    For List, you can continue like follows:
    List<string> arguements = new List<string>();
    arguements.add("C:\\Users\\All Users\\Apple\\Some File.msi");
    arguements.add("C:\\Users\\All Users\\Apple\\Another FIle.msi");

    and then to access the values you can just use it like u do it in the case of arrays i.e.
    to access the first element you can use arguements[0] and similarly arguements[1] for accessing the second element.

    Arraylist is exactly similar as List in terms of usage.

    P.S. You will need to import the System.Collections.Generic namespace to use the Lists in your code.
    CodeNameVirus

Similar Threads

  1. Modifying a raid array
    By Blunted One in forum Hardware
    Replies: 3
    Last Post: May 6th, 2007, 08:25 AM
  2. Perl Tutorial, Part 2
    By ch4r in forum Other Tutorials Forum
    Replies: 0
    Last Post: May 30th, 2005, 09:29 PM
  3. Perl Tutorial, Part 1
    By ch4r in forum Other Tutorials Forum
    Replies: 0
    Last Post: May 30th, 2005, 09:23 PM
  4. Arrays Tutorial (+free source)
    By ntsa in forum Other Tutorials Forum
    Replies: 0
    Last Post: September 13th, 2002, 09:10 AM
  5. Arrays made easy
    By zepherin in forum Other Tutorials Forum
    Replies: 4
    Last Post: July 13th, 2002, 07:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •