hello every one i'm a new member in this web site i'm really happy coz i found the best web to
leran the computer language it is will be better if some one explean for me the array in c# and syntax of using
array , i hope some one do it
thanks alot (DANA)

Dani AI

Generated

A compact, practical primer that builds on 's helpful start and the official guide pointed to.

Arrays in C# are reference types whose instances hold a fixed number of elements of the same type. When you create an array you set its rank (dimensions) and each dimension's length; those lengths stay constant for that instance and elements are initialized to their type default. Indexing is zero-based, so valid indexes run from 0 to Length-1. (learn.microsoft.com)

Common forms and syntax (short examples):

int[] primes = { 2, 3, 5, 7 };     // initializer for a single-dim array
int[,] grid = new int[3,4];        // rectangular 2D array
int[][] jagged = new int[3][];     // jagged array (array of arrays)

Use jagged arrays when sub-arrays can vary in length; use multi-dimensional (rectangular) arrays when you need true 2D indexing. Array initializers and these forms are part of the language specification. (learn.microsoft.com)

Useful operations and pitfalls: use the array properties/methods such as Length, Rank and GetLength to inspect bounds. To copy or move elements use Array.Copy or Clone; to change the size of an array you can use Array.Resize (it replaces the reference with a new array); these helpers are available on System.Array. Watch for IndexOutOfRangeException when you go past valid indexes. (learn.microsoft.com)

When you need a resizable sequence, prefer List<T> (it grows automatically and exposes convenient APIs like Add, Insert, ToArray). Arrays remain the right choice when you need contiguous memory, a fixed-size buffer, or slightly lower allocation overhead. For practical learning, step through small examples: create, print Length, try a foreach and a for loop, and experiment converting between List<T> and T[] with ToArray(). (learn.microsoft.com)

Quick troubleshooting notes: always check for null before indexing, validate indexes (>=0 and < Length), and prefer foreach for read-only iteration to avoid off-by-one mistakes.

Recommended Answers

All 3 Replies

An array is something that can hold a lot values , For instance you can store one value in a variable

int a=5;
string z="bob";
char b='b';

but in a array you can store a lot of values , for instance

int [] array1=new int [3];
array1[0]=3;
array1[1]=2;
array1[2]=5;

the syntax of an array goes like this

type_of_variable [] array_name=new type_of_variable [number of elements in array];

the new keyword is here to initialize the array and you must use it.
the sign [] means that the array is one dimensional
for example if you would have this sign [,] it would be two dimensional and so on...
i suggest you to go to this site for more information
One other thing ,, array1[0] the [0] is the first array address it just means that you are storing a value the first element in the first element of the array which always has a value of 0 . The last element in the array will always have (array_size-1) address
http://www.dotnetperls.com/array
Good luck

thanks alot for all of you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.