Click here to Skip to main content
15,908,111 members
Home / Discussions / C#
   

C#

 
GeneralRe: The current index in the foreach Pin
Judah Gabriel Himango27-Jul-05 11:10
sponsorJudah Gabriel Himango27-Jul-05 11:10 
GeneralRe: The current index in the foreach Pin
Matt Gerrans27-Jul-05 12:55
Matt Gerrans27-Jul-05 12:55 
GeneralRe: The current index in the foreach Pin
Judah Gabriel Himango27-Jul-05 13:49
sponsorJudah Gabriel Himango27-Jul-05 13:49 
GeneralRe: The current index in the foreach Pin
jan larsen27-Jul-05 20:57
jan larsen27-Jul-05 20:57 
GeneralRe: The current index in the foreach Pin
jan larsen27-Jul-05 11:13
jan larsen27-Jul-05 11:13 
GeneralRe: The current index in the foreach Pin
Libor Tinka27-Jul-05 11:44
Libor Tinka27-Jul-05 11:44 
GeneralRe: The current index in the foreach Pin
jan larsen27-Jul-05 20:38
jan larsen27-Jul-05 20:38 
GeneralRe: The current index in the foreach Pin
Patric_J28-Jul-05 4:47
Patric_J28-Jul-05 4:47 
cazzz wrote:
Is there any reason to use a foreach instead off a for loop?

Using foreach will automatically cast an item in the collection to right type.
Example:

for (int i = 0; i < 8; ++i)
{
    // You need to cast.
    Customer cust = (Customer)customers[i];
}


instead

foreach (Customer cust in customers)
{
    // cust is now of correct type Customer.
} 


Use foreach when you just want to iterate over all items and don't care about order or which item is the current item. Handle special behaviour inside a class and not in the calling code according to classic OO principles.
Example:

// Calling code somehow knows that customers[4] needs some special handling.
for (int i = 0; i < 8; ++i)
{
   if (i == 4)
   {
      Customer cust = (Customer)customers[i];
      DoSumthingWithCustomer(cust);
   }
}


instead

// Calling code only knows it needs to call a method for each customer object.
foreach (Customer cust in customers)
{
    // cust is now of correct type Customer.
    cust.DoSumthing();
}


You should implement the class Customer so when calling method DoSumthing() on its objects it will only do something for the object which have index 4 in the for loop.

Ugly indexing is normally not needed in my experience.


/Patric
My C# blog: C# Coach
GeneralRe: The current index in the foreach Pin
jan larsen28-Jul-05 8:02
jan larsen28-Jul-05 8:02 
Generaltextbox processed by enter key Pin
Anonymous27-Jul-05 10:20
Anonymous27-Jul-05 10:20 
GeneralRe: textbox processed by enter key Pin
Alomgir Miah27-Jul-05 10:30
Alomgir Miah27-Jul-05 10:30 
GeneralRe: textbox processed by enter key Pin
Anonymous27-Jul-05 10:48
Anonymous27-Jul-05 10:48 
GeneralRe: textbox processed by enter key Pin
jan larsen27-Jul-05 10:59
jan larsen27-Jul-05 10:59 
GeneralRe: textbox processed by enter key Pin
Guffa27-Jul-05 11:00
Guffa27-Jul-05 11:00 
Generalomg, what a cry baby! Pin
Anonymous27-Jul-05 11:45
Anonymous27-Jul-05 11:45 
GeneralRe: omg, what a cry baby! Pin
Guffa27-Jul-05 23:59
Guffa27-Jul-05 23:59 
QuestionHow can I.. Pin
KORCARI27-Jul-05 9:54
KORCARI27-Jul-05 9:54 
AnswerRe: How can I.. Pin
Alomgir Miah27-Jul-05 10:32
Alomgir Miah27-Jul-05 10:32 
GeneralRegular Expression Pin
eggie527-Jul-05 9:47
eggie527-Jul-05 9:47 
GeneralRe: Regular Expression Pin
Dan Neely27-Jul-05 10:38
Dan Neely27-Jul-05 10:38 
GeneralRe: Regular Expression Pin
Werdna27-Jul-05 11:13
Werdna27-Jul-05 11:13 
GeneralInheritance instances Pin
zapap27-Jul-05 9:31
zapap27-Jul-05 9:31 
GeneralRe: Inheritance instances Pin
Patric_J28-Jul-05 5:10
Patric_J28-Jul-05 5:10 
GeneralRe: Inheritance instances Pin
zapap28-Jul-05 8:56
zapap28-Jul-05 8:56 
Generalcreating new desktop Pin
27-Jul-05 9:08
suss27-Jul-05 9:08 

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.