473,698 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5941
Micus <No**@nowhere.c om> 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.co m>
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.c om> schrieb im Newsbeitrag
news:%2******** ********@TK2MSF TNGP11.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************ *************@g mx.de> wrote in message
news:%2******** *******@tk2msft ngp13.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.c om> schrieb im Newsbeitrag
news:%2******** ********@TK2MSF TNGP11.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
2128
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 using Python for a grand total of about a fortnight now. Hence, I apologise if what follows is a stupid suggestion, or if its already been made somewhere else, or if this is not the appropriate place to make it, etc. But I did honestly do some...
12
1965
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 variable without the var keyword. As such the code below works fine: <html>
9
1364
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: http://martinfowler.com/bliki/CollectionClosureMethod.html >. find can be defined as: def find(self, test = None): for item in self: if test: if test(item):
9
6360
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 keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this? Bryan Parkoff
5
16602
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 extern storage class: /****************************************************************** SOURCE FILE ONE *******************************************************************/ extern int i; /* Reference to i, defined below */
6
2454
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 for just declaring at the top of say, an aspx page, and then instantiating when you need it in various functions in the page? 2. I can't seem to figure out a way to use the NEW keyword to instantiate a class coming from a session object. ie,...
8
13300
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 that is being hidden in the base class. In VB.NET, the shadows keyword hides all the inherited functions, regardless of signature. I need this functionality in C#.
32
3035
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 the usage of static functions. I've been programming for years and I never felt a real need for the "static approach for functions". I don't know if this is a trivial matter since the reserved word
10
5118
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 not when I use a "normal" function: return len(object)
0
2983
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, int *restrict y) { *x = 0; *y = 1; return *x;
0
9169
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.