How To

How to Create an Array in C#

Contributor
By eHow Contributing Writer
(6 Ratings)

An array in C# is a collection of variables that all have the same data type. It can be accessed by using the name of the array and the index number that equals the individual item's position in the array. In addition to being addressable memory, a C# array is also an "object" that inherits from the array base class, allowing access to all its parent's methods and properties. This improved functionality allows run time resizing, inserting and other capabilities that were not possible with the legacy type of array whose size was fixed at compile time.

Difficulty: Moderately Easy
Instructions

Things You'll Need:

  • C# integrated development environment
  • C# compiler

    Create a Console Application Project

  1. Step 1

    Install Visual Studio (see Resources below). From the main window's "File," select "New," then select "Project." In the "New Project" Dialog Box that appears, expand the "C# Project Type" and highlight "Windows."

  2. Step 2

    Highlight "Console Application" under "Visual Studio Installed Templates" in the "Templates" pane. Click "OK" to create the project.

  3. Step 3

    Locate the "Main" method in the "Program.cs" file. Note the curly braces which are below it. Your code must be written between these opening and closing braces. It's alright to make some new, empty lines in there by positioning the cursor after the first brace and then pressing "Enter" a number of times.

  4. Create a C# Array

  5. Step 1

    Start to create an array in C# by typing the data type of your array, followed by an opening square bracket, then a closing square bracket, in the newly created space. An example, using the "int" data type, looks like this:
    int[]

  6. Step 2

    Type a blank space on the same line, and then type the name you will use for your new array. For example:
    int[] myIntArray

  7. Step 3

    Continue on the same line by typing a blank space, an "equals" sign and another blank space. Type the "new" keyword followed again by the data type and an opening and closing square bracket. So far, your line should look like this:
    int[] myIntArray = new int[]

  8. Step 4

    Initialize your array with some data. Use a comma-separated list enclosed in curly braces followed by a semicolon. Here is an example using the integers 1 to 5:
    {1, 2, 3, 4, 5}

  9. Step 5

    Verify that your C# array declaration is complete. It should look like this:
    int[] myIntArray = new int[] { 1, 2, 3, 4, 5 };

Tips & Warnings
  • String or character data types require each initialization value to be enclosed in quotes.

Post a Comment

Post a Comment

Have you done this? Click here to let us know.

I Did This

Related Ads

Internet
Virginia DeBolt,

Meet Virginia DeBolt eHow’s Internet Expert.

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US

Demand Media
eHow_eHow Technology and Electronics