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

Can I call this variable of foreach in C#?

meomap0z1
P: 7
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!!
3 Weeks Ago #1
Share this Question
Share on Google+
2 Replies


dbrewerton
100+
P: 114
If you use sv instead of s, exactly what happens? Does it error out?
2 Weeks Ago #2

meomap0z1
P: 7
Actually I do not have any problem for using sv instead of s
2 Weeks Ago #3

Post your reply

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