This site was last updated:Monday 13 October 2008, 11:30 GMT

Educational Community

ARRAYS IN C#
(5 votes, average: 5.00 out of 5)
Written by Christopher   
Sunday, 15 June 2008 18:39

INTRODUCTION

When defining variables you can choose between scalar number and arrays. Arrays in C# are declared by adding a set of square brackets to the end of the variable declaration. All members of an array must be of the same type. You cannot have different types in an array.

USING ARRAYS

An example of an array declaration is the following:

 

int[] integers;

This array is not initialized and we don’t know its dimensions. In order to declare and initialize the array you have to use the following statement:

int[] integers  =  new int[18];

The individual elements in this array are value types. However, the array itself is a reference type. It follows reference semantics and usage. If you assign one array variable to another it will just copy the reference to it and not the array. All C# arrays are zero-based. The first element has zero index value. To access the second element of the array you reference it with the index value 1:

                int a = integers[1];

Since the declaration of an array does not instantiate it we can use this to our advantage. We can declare an array and initialize it later in another portion of our code. This allows for dynamically-sized arrays. This technique assigns the null reference value to the array and later, when initialized, this reference is assigned the address of a new memory block of data requested with new keyword.

The array class contains several members for working with its contained data. Common methods include:

·         Clear() : Sets the specified elements of the array to zero, false or null depending the data type.

·         Copy(): Copies a range of elements from a source array to a destination array starting at specified indexes for both arrays.

·         ConstrainedCopy():Copies a range of elements from a source array to a destination array starting at specified indexes for both arrays. Guarantees that all changes will be undone if the copy is not successful.

·         Exists() :  Returns true or false. It searches to see if the array contains elements that match the conditions defined in the arguments of the method.

·         FindIndex(): searches the array for elements matching the conditions defined in the arguments of the method. It returns the zero-based index of the first occurrence.

·         Reverse(): Reverses the sequence of elements in the specified array.

·         Sorrt(): Sorts the elements of the array according to the keys of the elements.

·         TrueforAll(): Returns a Boolean value. True if all elements of the array match the conditions specified in the method’s arguments.

Trackback(0)
Comments (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 

User Menu

None
Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor KNOGE.com offers free video and text tutorials on various softwares also free resources to improve your economy and start making money online. THIS IS ONLY A TEST AD TO DISPLAY HOW IT MIGHT LOOK Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor

Adobe InDesign CS3 - Working with objects

Objects in Adobe InDesign CS3 are the basic building block of any design that you do. In this video tutorial you will learn how to create those blocks easily in...

InDesign Videos | Christopher

Read More

Adobe InDesign CS3 - Working with Panels & More

In this Adobe InDesign CS3 video tutorial you will learn how to set keys and work with panels, you will also learn how to create customized keyboard shortcuts for optimized...

InDesign Videos | Christopher

Read More

Adobe Photoshop CS3 - Create customized rust on cars

This Adobe Photoshop CS3 video tutorial will teach you how to create your own customized rust on cars for a more grungy and perhaps dusty effect. This technique does also...

Photoshop Videos | Christopher

Read More
100%
-
+
3
Show options