C# newbie

G

Guest

Hi,
if a funcion/defines is defines as follows

new private void Page_Load(object sender, System.EventArgs e)
{

}

is it C++ operator new?

thxs
newbie
 
M

Martin Eyles

new is a C# keyword, but you don't use it when you define your
method/function/subroutine. You should use what you have without the "new".

If you make objects, then you will use new to instantiate them, and new
behaves like c++ then.
 
K

Kevin Spencer

new is a C# keyword, but you don't use it when you define your
method/function/subroutine. You should use what you have without the
"new".

Not exactly. Actually, the "new" modifier in C# can be used in a method
definition, field definition, property definition, or nested class
definition, to hide ("Shadow" in VB.Net) an existing member with the same
name in the base class. It is similar to "override," except that it is used
to hide a member that is not overridable.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
K

Kevin Spencer

Well, not exactly. It is used for both name hiding (not overriding) and for
instantiating new instances of classes.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
G

Guest

so, "new" keyword it does name hiding without overloading in C# as well as
create new instances of classes like in c++.

thanks
newbie
 
K

Kevin Spencer

Almost there. Overloading and overriding are 2 different concepts.
Overloading is creating multiple methods with the same name and different
signatures (parameters, return values, etc). Overriding is creating a method
in a derived (inherited) class that is used instead of the same overridable
method in its base class. Overriding can only be used with overridable
methods in the base class. Methods not marked as overridable cannot be
overridden. The "new" modifier hides a member (not confined to methods) in
the base class, regardless of whether or not it is overridable.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top