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 use static an override method in C#?

meomap0z1
P: 7
I have this method in Student.cs :
Expand|Select|Wrap|Line Numbers
  1.  
  2. public virtual void XemThongTin()
  3.         {
  4.  
  5.             Console.WriteLine("Danh sach sinh vien: ");
  6.  
  7.         }
  8.  
then I override this method in another class which is Program.cs :
Expand|Select|Wrap|Line Numbers
  1. public override void XemThongTin()
  2.         {
  3.             Console.WriteLine("Danh sach sinh vien: ");
  4.             foreach (SinhVien sv in QuanLySinhVien.DanhSachSinhVien)
  5.             {
  6.                 Console.WriteLine("StudentID: " + sv.MMaSV);
  7.                 Console.WriteLine("Name " + sv.Name1);
  8.                 Console.WriteLine("DateOfBirth: " + sv.Birthday1);
  9.                 Console.WriteLine("Number: " + sv.Number1);
  10.                 Console.WriteLine("------*****------");
  11.             }
  12.         }
  13.  
Now I wanna call it in
static void Main(string[] args) {}
4 Weeks Ago #1

✓ answered by Ishan Shah

Yes, We can use static an override method in c#. I have tried it and I made MainClass for calling it in static void main(string[] args){}.

The Code is Following :

Expand|Select|Wrap|Line Numbers
  1. class MainClass
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             Student st=new Student();
  6.             st.XemThongTin();
  7.             Program pr=new Program();
  8.             pr.XemThongTin();
  9.             Console.ReadLine();
  10.         }
  11.  
  12.     }

Share this Question
Share on Google+
2 Replies


P: 5
Yes, We can use static an override method in c#. I have tried it and I made MainClass for calling it in static void main(string[] args){}.

The Code is Following :

Expand|Select|Wrap|Line Numbers
  1. class MainClass
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             Student st=new Student();
  6.             st.XemThongTin();
  7.             Program pr=new Program();
  8.             pr.XemThongTin();
  9.             Console.ReadLine();
  10.         }
  11.  
  12.     }
4 Weeks Ago #2

meomap0z1
P: 7
Wow, Thanks a million for your helping !!!!! You save my day, bro.
4 Weeks Ago #3

Post your reply

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