6 Replies - 349 Views - Last Post: 30 May 2019 - 01:17 PM Rate Topic: -----

#1 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Foreach (book apart in books)

Posted 30 May 2019 - 11:43 AM

What is the other ways of writing this to make it possible for the console application to write?

I remember it was something like foreach (int i, i++)

Please correct me someone.
Is This A Good Question/Topic? 0
  • +

Replies To: Foreach (book apart in books)

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,464
  • Joined: 12-June 08

Re: Foreach (book apart in books)

Posted 30 May 2019 - 11:44 AM

I'm not following. What are you asking for?
Was This Post Helpful? 0
  • +
  • -

#3 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Foreach (book apart in books)

Posted 30 May 2019 - 11:48 AM

I made a list of a book. Now I need console application to type what the book holds.

So far

foreach (book apart in books)
{Console.writeline(apart)}

Is written. I need the other type that goes something like

foreach (int I, I++)

Console.writeline(I++)

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,464
  • Joined: 12-June 08

Re: Foreach (book apart in books)

Posted 30 May 2019 - 11:51 AM

Yes.. ForEach is one.. as is for, do-while, and while. Then there's slightly more odd ball ones using Linq or recursion.

https://docs.microso...tion-statements
Was This Post Helpful? 0
  • +
  • -

#5 DarenR   User is offline

  • D.I.C Lover

Reputation: 617
  • View blog
  • Posts: 4,108
  • Joined: 12-January 10

Re: Foreach (book apart in books)

Posted 30 May 2019 - 12:32 PM

i find the most useful one is the foreach loop because it is a cleaner form of the loop

you can also use for(int i = 1; i<something.length; i++)

while aNumber is < something.length

etc
Was This Post Helpful? 0
  • +
  • -

#6 h4nnib4l   User is offline

  • The Noid
  • member icon

Reputation: 1399
  • View blog
  • Posts: 1,989
  • Joined: 24-August 11

Re: Foreach (book apart in books)

Posted 30 May 2019 - 01:16 PM

c# foreach syntax
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: Foreach (book apart in books)

Posted 30 May 2019 - 01:17 PM

It really depends on what data type your books is.

If books is an array, then you can do:
for(int i = 0; i < books.Length; i++)
    Console.WriteLine(books[i]);



If books implements ICollection, ICollection<T>, IReadOnlyCollection<T> like a List then you can do:
for(int i = 0; i < books.Count; i++)
    Console.WriteLine(books[i]);



If books implements IEnumerator or IEnumerable<T> then you can do:
for(var enumerator = books.GetEnumerator(); enumerator.MoveNext(); )
    Console.WriteLine(books.Current);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1