Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have code below,
its working fine without using "new" keyword, then why new keyword has been introduced!!
C#
namespace Question
{
	public class Program
	{
		public void Display ( )
		{
			Console.WriteLine( "Program" );
		}
	}

	public class ProgramB : Program
	{
		public void Display ( )
		{
			Console.WriteLine( "ProgramB" );
		}

		static void Main ( string[ ] args )
		{
			Program obj = new Program( );
			ProgramB objb = new ProgramB( );

			obj.Display( );
			objb.Display( );

			Console.ReadKey( );
		}
	}
}

OutPut:
Program
ProgramB
Posted
Updated 17-Jan-15 21:30pm
v2
Comments
Kornfeld Eliyahu Peter 18-Jan-15 3:31am    
But you ARE using new!? So what the question is?
Tomas Takac 18-Jan-15 3:40am    
There is compiler warning, which says you should use new keyword. But you are right, it works like this just fine.
Vivek S Kale 18-Jan-15 23:25pm    
so why it gives only warning why not compiler error

As stated in MSDN[^] the new modifier is only used to explicitly hide the base member with the same name and replace it with the derived one. There is no other purpose than suppressing the compiler warning. Your code will work as it is now, check the compiler output for the warning though. The warning is there to let you know that you may accidentally be hiding the base member. Using new you are basically saying to the compiler that you know what you are doing.
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 18-Jan-15 3:56am    
I like it, 5...
You are using new:
C#
Program obj = new Program( );
ProgramB objb = new ProgramB( );
And without them, your program woudln't work.

Let's just step back a bit and look at what new does.
When you declare a variable:
C#
Program p;
You allocate memory for a reference to a Program object: that doesn't create an instance of a Program class. That isn't created until you explicitly say "give me a Program instance" via the new keyword:
C#
p = new Program();

It's like a car parking space: it's a variable that can contain a car, but until you park there it is empty: you can't leave your house, start the car, and drive away. The new keyword "parks the car in the parking space" and now you can drive to the shops!
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 18-Jan-15 3:45am    
IMHO, OP's question is about the Display method...He does not understand how it works without adding new keyword to the method in ProgramB...
You have to learn about how Polymorphism is in OOP and in C# - http://msdn.microsoft.com/en-us/library/ms173152.aspx[^]
Under that tree there is a specific page addressing your question: http://msdn.microsoft.com/en-us/library/ms173153.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900