By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,369 Members | 1,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,369 IT Pros & Developers. It's quick & easy.

Can I call this variable of foreach in C#?

meomap0z1
P: 9
I have this code
Expand|Select|Wrap|Line Numbers
  1. class QuanLySinhVien
  2. {
  3. public static List<SinhVien> DanhSachSinhVien = new List<SinhVien>(50);
  4.         public List<SinhVien> GetDanhSachSinhVien
  5.         {
  6.             get => DanhSachSinhVien;
  7.         }
  8. public void FindStudent()
  9.         {
  10.             Console.Write("Enter ID's student that you need to find: ");
  11.             string msSV = Console.ReadLine().ToUpper();
  12.             SinhVien sv = CheckExist(msSV);
  13.             if (sv == null)
  14.             {
  15.                 Console.Write("Can not find this student with {0}'s ID\n", msSV);
  16.             }
  17.             else
  18.             {
  19.                 foreach (SinhVien s in QuanLySinhVien.DanhSachSinhVien)
  20.                 {
  21.                     Console.WriteLine("ID: " + sv.MMaSV);
  22.                     Console.WriteLine("Name: " + sv.HoTen1);
  23.                     Console.WriteLine("Birthdate: ");
  24.                     sv.NgaySinh1.ShowBirthdate();
  25.                     Console.WriteLine("Address: " + sv.DiaChi1);
  26.                     Console.WriteLine("Number: " + sv.DienThoai1);
  27.                     Console.WriteLine("------*****------");
  28.                     break;
  29.                 }
  30.  
  31.             }
  32.         } 
  33. }   
  34.  
My question that I wanna use this " SinhVien sv " in forEach
Expand|Select|Wrap|Line Numbers
  1.  foreach (SinhVien s in QuanLySinhVien.DanhSachSinhVien) }
  2.  
instead of using SinhVien s.
I do not use "SinhVien s" for printing the student on the list that I need to find.
Thanks for reading and helping me, have a nice day!!
Jan 30 '20 #1
Share this Question
Share on Google+
3 Replies

dbrewerton
100+
P: 115
If you use sv instead of s, exactly what happens? Does it error out?
Feb 5 '20 #2

meomap0z1
P: 9
Actually I do not have any problem for using sv instead of s
Feb 6 '20 #3

Frinavale
Expert Mod 5K+
P: 9,735
Do you need the foreach at all if you just want to print the details for a single, known/found object?
3 Weeks Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.