CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2013

    problem to understand " new virtual "

    to explain i will write a little code:
    class A {
    public virtual void f() { print "1"; }
    class B : A {
    public override void f() { print "2"; } 
    main() {
    A bb = new B();
    bb.f() ;
    in first case it will print 2
    but if i replace the word " override" with " new" then it will print 1
    why is it? why when i set method as new virtual it exicute the method that implemented in the base class ( generic)
    how it work exactly?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004

    Re: problem to understand " new virtual "

    Search bing or google for "c# new virtual tutorial".

    In general, when learning C#, you are going to need to do or find several examples on the internet or in a book in order to understand the concepts. Please try to find the answers on your own before posting to the forum. I'm definitely not saying we don't want to help, but in many cases with regard to the initial learning of C#, an online tutorial is going to be able to explain the subject in more depth than we can. If one tutorial doesn't make things clear, then do other tutorials (because each one will explain things slightly different).

    Btw, one of the best things that you can do as a beginning programmer is to learn how to find the information yourself. That way, when you are stuck with a difficult problem and under a time crunch, you won't have to wait for someone else to answer your question. It's never good to have to tell a boss, "I don't know what the problem is and I'm waiting for help on the internet."

  3. #3
    Join Date
    Oct 2004
    Rocket City

    Re: problem to understand " new virtual "

    A bb = new B();
    Did you intend to use:
    B bb = new B();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.