473,396 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

use of 'new' keyword in functions

I was wondering what the thought behind the 'new' keyword was in C# when
applied to a function? For Example :

public class A
{ ...
public virtual void Func() { // impl_A}
.... }

public class B : A
{ ...
public override void Func() { // impl_B}
.... }

public class C : B
{ ...
public override void Func() { // impl_C}
.... }

public class D : B
{ ...
public new void Func() { // impl_D}
.... }

// use classes - polymorphism
A[4] objArray = {new A(), new B(), new C(), new D() };
objArray[0].Func(); //impl_A called
objArray[1].Func(); //impl_B called
objArray[2].Func(); //impl_C called
objArray[3].Func(); //impl_B called *not impl_D

D objD = new D();
D.Func(); //impl_D called *

In what situation would you want this behavior? On the surface, it appears
the only time 'new' would make sense is when a class is inherited and you
want to 'override' a function, but the function is not overridden when
polymorphism is applied.???

I did find a previous post from Jan/04 covering this but it is no longer
available (sorry about the repeat).

Thanks for your time,
M
Nov 16 '05 #1
3 5930
Micus <No**@nowhere.com> wrote:
I was wondering what the thought behind the 'new' keyword was in C# when
applied to a function? For Example :


See http://www.pobox.com/~skeet/csharp/faq/#override.new

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
You use new when you want/must add a method to a class (e.g. when
implementing interfaces) and *do not* want to override the original one. But
you can also reach this behaviour with explicit interface implementation so
I really cannot say that I know an example where you actually want such a
behaviour.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
"Micus" <No**@nowhere.com> schrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP11.phx.gbl...
I was wondering what the thought behind the 'new' keyword was in C# when
applied to a function? For Example :

public class A
{ ...
public virtual void Func() { // impl_A}
... }

public class B : A
{ ...
public override void Func() { // impl_B}
... }

public class C : B
{ ...
public override void Func() { // impl_C}
... }

public class D : B
{ ...
public new void Func() { // impl_D}
... }

// use classes - polymorphism
A[4] objArray = {new A(), new B(), new C(), new D() };
objArray[0].Func(); //impl_A called
objArray[1].Func(); //impl_B called
objArray[2].Func(); //impl_C called
objArray[3].Func(); //impl_B called *not impl_D

D objD = new D();
D.Func(); //impl_D called *

In what situation would you want this behavior? On the surface, it appears
the only time 'new' would make sense is when a class is inherited and you
want to 'override' a function, but the function is not overridden when
polymorphism is applied.???

I did find a previous post from Jan/04 covering this but it is no longer
available (sorry about the repeat).

Thanks for your time,
M

Nov 16 '05 #3

"cody" <pl*************************@gmx.de> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
You use new when you want/must add a method to a class (e.g. when
implementing interfaces) and *do not* want to override the original one. But you can also reach this behaviour with explicit interface implementation so I really cannot say that I know an example where you actually want such a
behaviour.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
"Micus" <No**@nowhere.com> schrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP11.phx.gbl...
I was wondering what the thought behind the 'new' keyword was in C# when
applied to a function? For Example :

public class A
{ ...
public virtual void Func() { // impl_A}
... }

public class B : A
{ ...
public override void Func() { // impl_B}
... }

public class C : B
{ ...
public override void Func() { // impl_C}
... }

public class D : B
{ ...
public new void Func() { // impl_D}
... }

// use classes - polymorphism
A[4] objArray = {new A(), new B(), new C(), new D() };
objArray[0].Func(); //impl_A called
objArray[1].Func(); //impl_B called
objArray[2].Func(); //impl_C called
objArray[3].Func(); //impl_B called *not impl_D

D objD = new D();
D.Func(); //impl_D called *

In what situation would you want this behavior? On the surface, it appears the only time 'new' would make sense is when a class is inherited and you want to 'override' a function, but the function is not overridden when
polymorphism is applied.???

I did find a previous post from Jan/04 covering this but it is no longer
available (sorry about the repeat).

Thanks for your time,
M


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Jordan Rastrick | last post by:
First, a disclaimer. I am a second year Maths and Computer Science undergraduate, and this is my first time ever on Usenet (I guess I'm part of the http generation). On top of that, I have been...
12
by: Jack | last post by:
Hi, Is it recommended to use the var keyword when declaring a variable? IE lets me create a variable with var, then create the same one again with no problem. Also it lets me create a...
9
by: Mike Meyer | last post by:
Another thread pointed out a couple of methods that would be nice to have on Python collections: find and inject. These are taken from <URL:...
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
5
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining...
6
by: Jason Shohet | last post by:
2 questions for anyone who can answer: 1. in class declarations, I realize you have to use the NEW keyword if you want to declare & instantiate some class at the same time. Whats an advantage...
8
by: Dot net work | last post by:
I need VB.NET's "shadows" functionality inside a C# project. I tried the "new" keyword, but it didn't seem to work, because my particular function does in fact differ in signature to the function...
32
by: lcdgoncalves | last post by:
Hi everyone Is there a real need to use keyword static with functions, if we simply don't declare their prototypes in .h file? Many textbooks avoid to discuss this matter and/or discuss only...
10
by: Armando Serrano Lombillo | last post by:
Why does Python give an error when I try to do this: Traceback (most recent call last): File "<pyshell#40>", line 1, in <module> len(object=) TypeError: len() takes no keyword arguments but...
0
by: copx | last post by:
Restrict keyword questions How far does the guarantee that an object is not accessed through another pointer go? I mean, all examples I have seen are simple stuff like: int f (int *restrict x,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.