Tutorial - C# / Mono - Arrays


Coding Friends Tutorial Index - > C# / Mono

Author Ian - Tutorial Posts = 62
Arrays are ways of having a block of memory allocated to a single variable type, this allows for holding the national lottery numbers within an array of 6 instead of actually having 6 different variables, saves on variable overload

e.g.
int value1 = new int (1);
int value2 = new int (2);
int value3 = new int (3);
int value4 = new int (4);
int value5 = new int (5);
int value6 = new int (6);

and instead you could just have
int[] values = new int[6] {1,2,3,4,5,6};

This is example code of how to use arrays.
---
using System;

class arrays
{
       public static void Main()
       {
              // the [] means a array declaration, and when you create the new instance of a int you then define the size (3 in this case)
              // if you wish to define the default values, then place these within the {} brackes
              int[] arrayInt = new int[3] { 3,2,1};       
              
              // for each (integer in the array of integers assign to i)
              foreach (int i in arrayInt)
                     Console.WriteLine(i);

              // to alter the values within the array, an array always starts at 0
              arrayInt[0] = 20;
              arrayInt[1] = 30;
              arrayInt[2] = 40;

              foreach (int i in arrayInt)
                     Console.WriteLine(i);
              return;
       }
}
---
Save as array.cs and then compile using either mono/csc.exe. The output will be
3
2
1
20
30
40
Creation of cool tutorials :)
User Name Password
Copyright@CodingFriends, 2005-2006. All Rights Reserved.
Home | Forums | Tutorials | Users
RSS Feeds - Global Global CodingFriends RSS Feed - Tutorials Tutorials CodingFriends RSS Feed - Forums Forums CodingFriends RSS Feed - News News CodingFriends RSS Feed
Users
Login|Password problem| Register here

Tutorials
Tutorials Home| C/C++| C#/Mono| Java| Javascript| PHP| Ruby| SQL| (X)HTML/CSS| VB| Linux| Windows

Forum
Forum Home

Projects
3D Game

Site
Home| About me| Links| FAQ

Search