|
|
|
|
![]() ![]() |
Jun 8 2007, 03:32 AM
Post
#1
|
|
|
Premium Member Group: [HOSTED] Posts: 367 Joined: 16-February 06 From: Kolkata, India Member No.: 11,322 |
foreach statement
This statement is explicitly used to traverse through arrays. The benefit of using foreach over the normal for statement is that it is not needed to check the size of the array while using the former. Syntax:- foreach(type identifier in expression) { statement 1; statement 2; .... } Suppose we have an array StudentNames containing the name of all the students in a class. We need to display the name of each one of them on screen. First we will see how it can be done using the for loop. CODE string[] StudentNames = new string[] {"Peter", "Tony", "Bruce", "Scott", "Clark", "Kenshin"}; for (int I = 0; I < StudentNames.Length; I++) { Console.WriteLine(StudentNames[I]); } Now we'll use the foreach statement to do this. CODE string[] StudentNames = new string[] {"Peter", "Tony", "Bruce", "Scott", "Clark", "Kenshin"}; foreach (string StudentName in StudentNames) { Console.WriteLine(StudentName); } Param Arrays Remember the Console.WriteLine method which outputs the result using a string format followed by the additional values as input parameters. For example: Console.WriteLine("The sum of {0} and {1} is {2}", Num1, Num2, Num1 + Num2); would display The sum of 5 and 6 is 11 for values 5 and 6 for Num1 and Num2 respectively. What is worth noting here, is that WriteLine() can take any number of parameters after the string format. So, something like this would work as well:- Console.WriteLine("The Prime Numbers -> {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}", 2, 3, 5, 7, 11, 13, 17, 19, 23); This is done by the param array data type. Example:- CODE public static int Sum(params int[] Numbers) { int Sum = 0; foreach (int Number in Numbers) { Sum += Number; } return Sum; } This is a function to return the sum of any number of numbers. The numbers to be summed are passed as parameters during the function call as: Sum(4, 69, 13, 99); Note: param array should be the last parameter in the function signature which also means that only one param array can be used in a function. Why is this done? Consider the following function signature: public static int MyFunction(params int[] A, int C). How would we call the function if this were to be allowed? MyFunction(1, 2, 3, 4, 5); How can the compiler determine where the values for the param array A finish? Actually, if a reverse scanning of parameters were to be done, this could be determined. But that would increase useless complexity of having to implement this along with the normal forward scanning of parameters. Multi Dimensional Arrays Up until now, we have only worked with Single Dimensional arrays. Now we shall see how the Multi Dimensional arrays are declared, initialized and manipulated. The pictures below show how the Single and Double Dimensional arrays are represented. ![]() ![]() While, the Single Dimensional arrays are represented as a single row of elements, Double Dimensional arrays are represented by multiple rows. Similarly, arrays with 3 dimensions would be represented in 3D space along the three axes. Declaration Declaration is done by the syntax:- datatype [ , ] VariableName; The number of commas ( , ) inside the square brackets [ ] should be one less than the number of dimensions required for the array. Example Usage:- int [ , ] Numbers; Initialization Initialization is done in a way where each row is treated like a single dimensional array. Example:- CODE int[,] Numbers = new int[3, 3] { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; Assigning Values Values can be assigned as:- Numbers[1, 2] = 7; Array Class The Array class defined in the System namespace serves as the base class for all the arrays in the Common Language Runtime. It can be used to create, manipulate, search and sort arrays. Some of the common Properties and methods are summarized below. Properties
An integer array can only store integers. Same goes for any other array type - string, boolean, etc. Collections overcome this limitation of arrays and can store elements of different types as items. This is possible because Collections actually store references and not values. We use the System.Collections namespace to work with collections. Boxing - No, not the Sport! Boxing is the automatic conversion of value types to reference types allowing them to be stored in collections. Unboxing - The reverse of the former, conversion of reference type to value type. Because Collections store references, it is necessary to do the above conversions while storing and retrieving values in Collections. List of classes under the System.Collection namespace. Array List - Array List is a better alternative to arrays because of the following advantages it offers over arrays.
Lesson: 1 2 3 4 5 6 7 This post has been edited by turbopowerdmaxsteel: Jun 9 2007, 04:06 AM |
|
|
|
Mar 2 2008, 09:10 AM
Post
#2
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 0 Joined: 1-November 07 Member No.: 25,869 |
Generating Random Numbers
C# Tutorial : Lesson 7 - Creating Value Types & Reference Types - Part II Ive been trying to generate random integers in a C program but I cant. Please help me do this and send me the code. The program should create a list of about 50 integers and assign them to a an initialized list. Which I can view the list and choose whether to erase it. Thank you in advance -reply by Boniface Kegode |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 30th August 2008 - 04:08 AM |