Subscribe to Subscribers
Talk to Webmaster Tony John


Resources » .NET programming » .NET Framework

Arrays


Posted Date:     Category: .NET Framework    
Author: Member Level: Gold    Points: 7


An array is a data structure that contains a number of variables which are accessed through computed indices.



 


ARRAY



An array has a rank which determines the number of indices associated with each
array element. The rank of an array is also referred to as the dimensions of
the array. An array with a rank of one is called a single-dimensional
array, and an array with a rank greater than one is called a multi-dimensional array.

Each dimension of an array has an associated length which is an integral number greater than or equal to zero. The dimension lengths are not part of the type
of the array, but rather are established when an instance of the
array type is created at run-time. The length of a dimension determines the valid range of indices for that dimension: For a dimension of length N, indices
can range from 0 to N – 1 inclusive. The total number of elements in an array
is the product of the lengths of each dimension in the array. If one or more of the dimensions of an array have a length of zero, the array is said to be empty.

The element type of an array can be any type, including an array type.

Array types

An array type is written as a non-array-type followed by one or more rank-specifiers:

array-type:
non-array-type rank-specifiers
non-array-type:
type
rank-specifiers:
rank-specifier
rank-specifiers rank-specifier
rank-specifier:
[ dim-separatorsopt ]
dim-separators:
,
dim-separators ,

A non-array-type is any type that is not itself an array-type.
The rank of an array type is given by the leftmost rank-specifier in the array-type: A rank-specifier indicates
that the array is an array with a rank of one plus the number of "," tokens in the rank-specifier.
The element type of an array type is the type that results from deleting the leftmost rank-specifier:
· An array type of the form T[R] is an array with rank R and a non-array element type T.
· An array type of the form T[R][R1]...[RN] is an array with rank R and an element type T[R1]...[RN].


The System.Array type

The System.Array type is the abstract base type of all array types. An implicit reference conversion exists from any array type to System.Array, and an
explicit reference conversion System.Array to any array type. Note that System.Array is itself not an array-type. Rather, it is a class-type
from which all array-types are derived.
At run-time, a value of type System.Array can be null or a reference to an instance of any array type.+

Array creation


Array instances are created by array-creation-expressions or by field or local
variable declarations that include an array-initializer When an array instance
is created, the rank and length of each dimension are established and then remain constant for the entire lifetime of the instance.
In other words, it is not possible to change the rank of an existing array
instance, nor is it possible to resize its dimensions.
An array instance created by an array-creation-expression is always of an array type. The System.Array type is an abstract type that cannot be instantiated.

Array members

Every array type inherits the members declared by the System.Array type.

Array covariance

For any two reference-types A and B, if an implicit reference conversion or
explicit reference conversion exists from A to B, then the same reference
conversion also exists from the array type A[R] to the array type B[R],
where R is any given rank-specifier (but the same for both array types). This relationship is known as array covariance. Array covariance in particular means
that a value of an array type A[R] may actually be a reference to an
instance of an array type B[R], provided an implicit reference conversion
exists from B to A.
Because of array covariance, assignments to elements of reference type arrays
include a run-time check which ensures that the value being assigned to the
array element is actually of a permitted type.
For example:

class Test
{
static void Fill(object[] array, int index, int count, object value)
{
for (int i = index; i < index + count; i++) array[i] = value;
}
static void Main() {
string[] strings = new string[100];
Fill(strings, 0, 100, "Undefined");
Fill(strings, 0, 10, null);
Fill(strings, 90, 10, 0);
}
}


Array initializers

Array initializers may be specified in field declarations , local variable declarations, and array creation expressions

array-initializer:

{
variable-initializer-listopt
}
{
variable-initializer-list

}
variable-initializer-list:

variable-initializer
variable-initializer-list
variable-initializer
variable-initializer:
expression
array-initializer

An array initializer consists of a sequence of variable initializers, enclosed by "{"and "}" tokens and separated by "," tokens. Each variable initializer is
an expression or, in the case of a multi-dimensional array, a nested array initializer.
The context in which an array initializer is used determines the type of the array being initialized. In an array creation expression, the array type
immediately precedes the initializer. In a field or variable declaration, the
array type is the type of the field or variable being declared. When an array initializer is used in a field or
variable declaration, such as:

int[] a = {0, 2, 4, 6, 8};

it is simply shorthand for an equivalent array creation expression:

int[] a = new int[] {0, 2, 4, 6, 8}
.


two-dimensional array

creates a two-dimensional array with a length of five for the leftmost dimension and a length of two for the
rightmost dimension:
int[,] b = new int[5, 2];
and then initializes the array instance with the following values:

b[0, 0] = 0; b[0, 1] = 1;
b[1, 0] = 2; b[1, 1] = 3;
b[2, 0] = 4; b[2, 1] = 5;
b[3, 0] = 6; b[3, 1] = 7;
b[4, 0] = 8; b[4, 1] = 9;

When an array creation expression includes both explicit dimension lengths and an array initializer, the lengths
must be constant expressions and the number of elements at each nesting level must match the corresponding
dimension length. Some examples:

int i = 3;
int[] x = new int[3] {0, 1, 2};
int[] y = new int[i] {0, 1, 2};
int[] z = new int[3] {0, 1, 2, 3};

Reference http://naveenkumarm.page.tl
Related Resources:


Read related articles: Arrays    Two dimensional array in asp.net    Two dimensional array    Array list in asp.net using c#    Array in asp.net    Array list in asp.net    


Did you like this resource? Share it with your friends and show your love!


Responses to "Arrays"
Author: manisha    05 May 2010Member Level: Gold   Points : 0
good article and thanks for sharing with us.

-- Manisha Chaubey



Author: Naveen Kumar    05 May 2010Member Level: Gold   Points : 0
Thank you manisha ............



keep in Happy Coding...........

Regards,

Keepin touch in
http://naveenkumarm.page.tl/%3Ch4%3EHome%3C-s-h3%3E.htm
Naveen Kumar



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: 10 Steps to create a setup application
    Previous Resource: How to Add the Reference of a User Control in the web.config file in ASP.NET
    Return to Resources
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    An array is a data structure that contains a number of variables which are accessed through computed  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.