waht exactly the use of new keyword in c#
-
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
-
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
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;
-
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
Try this [^] article. Hope it helps.
There are 10 kinds of people: those who understand binary and those who don't
-
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
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 theprint
method, the method in theuser
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;
-
Try this [^] article. Hope it helps.
There are 10 kinds of people: those who understand binary and those who don't