C#: Array items in .NET
This sample code shows
you how to use arrays and access array items by using foreach loop.
[C#]
using System;
namespace ArraySample
{
class MyFirstArray {
static void Main(string[] args) {
int[] intUserID = new int[3];
intUserID[0] = 5;
intUserID[1] = 7;
intUserID[2] = 2;
Console.WriteLine(".:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.");
foreach (int i in intUserID) {
Console.WriteLine("UserID: " + i.ToString());
}
string [] strUserNames = new String[5]{"IRTAZA", "JUNAID", "ATIF",
"MAQSOOD", "WAQAS"}; Console.WriteLine(".:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.");
foreach (string str in strUserNames) {
Console.WriteLine("Username: " + str);
}
//Multi-dimensional string array
string[,] strFullNames = new string[,] { {"Syed Irtaza", "Ali"},
{"Junaid", "Ali"} }; Console.WriteLine(".:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.");
foreach (string str in strFullNames) {
Console.WriteLine("Fullname: " + str);
}
Console.ReadLine();
}
}
}<o:p></o:p>
<o:p> </o:p>
The output of the above code will look like this<o:p></o:p>
.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.
UserID: 5
UserID: 7
UserID: 2
.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.
Username: IRTAZA
Username: JUNAID
Username: ATIF
Username: MAQSOOD
Username: WAQAS
.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.
Fullname: Syed Irtaza
Fullname: Ali
Fullname: Junaid
Fullname: Ali<o:p></o:p>
Understanding the Array Class
The Array class, defined in the System namespace, is the base class for arrays
in C#. Array class is an abstract base class but it provides CreateInstance
method to construct an array. The Array class provides methods for creating,
manipulating, searching, and sorting arrays.
The System.Array Class Properties.
IsFixedSize Return a value indicating if an array has a fixed size or not. ="EN"> R
IsReadOnly Returns a value indicating if an array is read-only or not.
IsSynchronized
eturns a value indicating if access to an array is thread-safe or not.
Rank Returns the number of dimensions of an array.
SyncRoot Returns an object that can be used to synchronize access to the array.
The System.Array Class Methods.
BinarySearch This method searches a one-dimensional sorted Array for a value, using a binary search algorithm.
Clear This method removes all items of an array and sets a range of items in the array to 0.
Clone This method creates a shallow copy of the Array.
Copy This method copies a section of one Array to another Array and performs type casting and boxing as required.
CopyToThis method copies all the elements of the current one-dimensionalArray to the specified one-dimensional Array starting at the specifieddestination Array index.
CreateInstance This method initializes a new instance of the Array class.
GetEnumerator This method returns an IEnumerator for the Array.
GetLength This method returns the number of items in an Array.
GetLowerBound This method returns the lower bound of an Array.
GetUpperBound This method returns the upper bound of an Array.
GetValue This method returns the value of the specified item in an Array.
IndexOf This method returns the index of the first occurrence of a value in a one-dimensional Array or in a portion of the Array.
Initialize This method initializes every item of the value-type Array by calling the default constructor of the value type.
LastIndexOf This method returns the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array.
Reverse This method reverses the order of the items in a one-dimensional Array or in a portion of the Array.
SetValue This method sets the specified items in the current Array to the specified value.
Sort This method sorts the items in one-dimensional Array objects.












No comments yet... Be the first to leave a reply!