Click here to Skip to main content
15,899,754 members
Home / Discussions / C#
   

C#

 
Questionfee system with keep the record of whole year Pin
Haidar ali23-Jan-15 16:56
Haidar ali23-Jan-15 16:56 
AnswerRe: fee system with keep the record of whole year Pin
Dave Kreskowiak23-Jan-15 17:05
mveDave Kreskowiak23-Jan-15 17:05 
QuestionMulti Lingual C# application - Issue on getting Chinese Language selection option on typing English key combination Pin
Johnson Antony23-Jan-15 15:57
Johnson Antony23-Jan-15 15:57 
QuestionDifference between Array and Loop. Pin
Member 1116162523-Jan-15 3:08
Member 1116162523-Jan-15 3:08 
AnswerRe: Difference between Array and Loop. Pin
Wendelius23-Jan-15 3:17
mentorWendelius23-Jan-15 3:17 
AnswerRe: Difference between Array and Loop. Pin
PIEBALDconsult23-Jan-15 3:24
mvePIEBALDconsult23-Jan-15 3:24 
GeneralRe: Difference between Array and Loop. Pin
nagendrathecoder23-Jan-15 4:05
nagendrathecoder23-Jan-15 4:05 
AnswerRe: Difference between Array and Loop. Pin
LLLLGGGG23-Jan-15 10:52
LLLLGGGG23-Jan-15 10:52 
Hi,
they are two completely different things:
Array:
An array is a set of homogeneous variables (I.E. they share the same type). it is declared in this way:
C#
YourType[] yourArray = new YourType[noitems]

where noitems is the number of variables in the array.
To access a single variable you need to use an index. This index starts from 0, not from 1, so the array indicies will go from 0 to noitems-1!!
C#
YourType yourArrayVariable = yourArray[yourIndex];

Please keep in mind that if yourIndex points to a variable which does not exists in the array, a IndexOutOfRangeException will be thrown.
Arrays can also have multiple dimensions:
C#
YourType[,] yourTable = new YourType[6,2];

it is a declaration of a table with 6 rows and 2 columns. You can imagine a single-dimension array as a single row, a two-dimensional array as a table and a three dimensional array as a cube.

There are also jagged arrays. While if you decalre your multi-dimensional array just like the code above you'll get a rectuagular table, with jagged arrays (or array of arrays), you can define your personal dimension for each element.
C#
YourType[][] jaggedArray = new YourType[3][];
jaggedArray[0] = new YourType[3];    //<----- jaggedArray[0] is a single-dimensional array of type YourType
jaggedArray[0][2] //<----- This is a single variable of type YourType


Loops are a completely different thing.
A loop enables you to do something repeatedly while a condition is true.

While loops will do the instructions inside their body while a condition is true. The condition is evaluated before the loop starts and, if it is false, the loop does not start.
<pre lang="c#">
while (condition){
//do something if condition is true
}


There is another version of while which is the do...while loop. The only difference is that the condition is evaluated after the loop is executed. I.E. it is evaluated at the end, so the loop is executed at least once before evaluating the condition.

C#
do{
//do something
}while(condition);


There is the for loop which is controlled by a variable which is usually an integer or, generally, a number:
C#
for (declaration;condition;instruction){
//do something
}

for(int i = 0;i<5;i++){
}

The for loop has 3 different parts: the declaration of the control variable (actually, whatever instruction which has to be called before executing the first iteration of the loop), the condition (which is the condition to execute the loop as the other loops) and then the instruction, which is an instruction which is called at the end of each iteration. Usually, the for loop is used to increment a variable to access array elements.
C#
string[] lines = new string[10];
for(int i = 0;i<10;i++){
Console.WriteLine(lines[i]);
}

Please keep in mind that the variable declared inside the decalration block of the for is valid for the for loop only and it is not available outside it.
If you want to make it available do this:
C#
string[] lines = new string[10];
int i;
for(i = 0;i<10;i++){
Console.WriteLine(lines[i]);
}

And notice that the maximum value for i is 9 and not 10 as 10<10 = false.

The last loop is the best to deal with arrays. This is the foreach loop. This loop enables you to iterate over all the items in an array and store the current value in a variable.
This code will print all the numbers inside the array integers:
C#
int[] integers = new int[20];
foreach(int i in integers){
Console.WriteLine(i);
}


Unfortunately, the foreach loop has not an index variable and, if you want it, you'll have to declare it explicitely.

Hope this helps. Smile | :)
Lusvardi Gianmarco

QuestionWhy does the callback method of an EventHandler<T> need to be static? Pin
TMattC22-Jan-15 22:38
TMattC22-Jan-15 22:38 
QuestionRe: Why does the callback method of an EventHandler<T> need to be static? Pin
Richard Deeming22-Jan-15 23:34
mveRichard Deeming22-Jan-15 23:34 
AnswerRe: Why does the callback method of an EventHandler<T> need to be static? Pin
TMattC22-Jan-15 23:56
TMattC22-Jan-15 23:56 
GeneralRe: Why does the callback method of an EventHandler<T> need to be static? Pin
Eddy Vluggen23-Jan-15 0:35
professionalEddy Vluggen23-Jan-15 0:35 
QuestionDeleted Files path from HardDisk ( FAT / NTFS ) in C# Pin
Member 1139660822-Jan-15 22:35
Member 1139660822-Jan-15 22:35 
AnswerRe: Deleted Files path from HardDisk ( FAT / NTFS ) in C# Pin
OriginalGriff23-Jan-15 0:26
mveOriginalGriff23-Jan-15 0:26 
GeneralRe: Deleted Files path from HardDisk ( FAT / NTFS ) in C# Pin
deepankarbhatnagar23-Jan-15 1:17
professionaldeepankarbhatnagar23-Jan-15 1:17 
GeneralRe: Deleted Files path from HardDisk ( FAT / NTFS ) in C# Pin
Pete O'Hanlon23-Jan-15 2:59
mvePete O'Hanlon23-Jan-15 2:59 
QuestionEncrypting String Pin
EricSouthman22-Jan-15 21:59
EricSouthman22-Jan-15 21:59 
AnswerRe: Encrypting String Pin
V.22-Jan-15 22:39
professionalV.22-Jan-15 22:39 
AnswerRe: Encrypting String Pin
Eddy Vluggen23-Jan-15 0:33
professionalEddy Vluggen23-Jan-15 0:33 
AnswerRe: Encrypting String Pin
GuyThiebaut23-Jan-15 0:43
professionalGuyThiebaut23-Jan-15 0:43 
QuestionHow can I clear a file's content? Pin
kidult22-Jan-15 18:10
kidult22-Jan-15 18:10 
AnswerRe: How can I clear a file's content? Pin
Wendelius22-Jan-15 18:19
mentorWendelius22-Jan-15 18:19 
GeneralRe: How can I clear a file's content? Pin
kidult22-Jan-15 21:06
kidult22-Jan-15 21:06 
GeneralRe: How can I clear a file's content? Pin
Wendelius23-Jan-15 2:42
mentorWendelius23-Jan-15 2:42 
Questionc# validation Pin
Member 1135438622-Jan-15 13:51
Member 1135438622-Jan-15 13:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.