Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. waht exactly the use of new keyword in c#

waht exactly the use of new keyword in c#

Scheduled Pinned Locked Moved C#
csharphelpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sravani p
    wrote on last edited by
    #1

    heloo.... i want to hide the base class method using new operator. i tried this code.but it was not working.here base class method is calling. plz help me soon. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication23 { class Program:user { public new void print() { Console.WriteLine("hello how are you?"); } static void Main(string[] args) { user u = new Program(); u.print(); Console.ReadLine(); } } class user { public void print() { Console.WriteLine("hellohello"); } } } sravani.p

    A Y G 3 Replies Last reply
    0
    • S sravani p

      heloo.... i want to hide the base class method using new operator. i tried this code.but it was not working.here base class method is calling. plz help me soon. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication23 { class Program:user { public new void print() { Console.WriteLine("hello how are you?"); } static void Main(string[] args) { user u = new Program(); u.print(); Console.ReadLine(); } } class user { public void print() { Console.WriteLine("hellohello"); } } } sravani.p

      Y Offline
      Y Offline
      YouMiss
      wrote on last edited by
      #2

      Hey, the 'new' keyword is to explicitly hide the base class members But, if you want to use it, you have to call the method from the derived class. In this case you surely need to create a reference of the derived class other than a original base class reference. Since you have already created the Program object to a user reference, all you need to do is to perform a cast: (Program)u.print(); regards

      Someone was born greatness; Someone achieved greatness; Someone let the greatness thrust upon him;

      1 Reply Last reply
      0
      • S sravani p

        heloo.... i want to hide the base class method using new operator. i tried this code.but it was not working.here base class method is calling. plz help me soon. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication23 { class Program:user { public new void print() { Console.WriteLine("hello how are you?"); } static void Main(string[] args) { user u = new Program(); u.print(); Console.ReadLine(); } } class user { public void print() { Console.WriteLine("hellohello"); } } } sravani.p

        A Offline
        A Offline
        Andrei Ungureanu
        wrote on last edited by
        #3

        Try this [^] article. Hope it helps.

        There are 10 kinds of people: those who understand binary and those who don't

        S 1 Reply Last reply
        0
        • S sravani p

          heloo.... i want to hide the base class method using new operator. i tried this code.but it was not working.here base class method is calling. plz help me soon. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication23 { class Program:user { public new void print() { Console.WriteLine("hello how are you?"); } static void Main(string[] args) { user u = new Program(); u.print(); Console.ReadLine(); } } class user { public void print() { Console.WriteLine("hellohello"); } } } sravani.p

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          sravani.p wrote:

          i tried this code.but it was not working.here base class method is calling.

          It's working just fine. You have a reference to user, so when you use the print method, the method in the user class is used. If you want the method in the Program class to be used when you have a Program instance references by a reference to user, you have to use a virtual method instead of using the new modifier.

          --- single minded; short sighted; long gone;

          1 Reply Last reply
          0
          • A Andrei Ungureanu

            Try this [^] article. Hope it helps.

            There are 10 kinds of people: those who understand binary and those who don't

            S Offline
            S Offline
            sravani p
            wrote on last edited by
            #5

            thanks for ur answer,i tried but its not working.... sravani p

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups