new class modifier and tested class

F

fh1996

Documents say that "new" class modifier is only used with nested classes.
"new" indicates that the class hides an inherited member of the same name."

public class MyDerivedC : MyBaseC
{
new public void Invoke() {}
}

The only thing I don't understand in the above statement and example is that
it says that "new" only used with nested classes. However the example isn't
nested class; it's an inheritance. An example of nested class should be
something like:

class NestedClass
{
class MyClass
{
public string name;
public int id;

public MyClass ()
{
}

public MyClass (int id, string name)
{
this.id = id;
this.name = name;
}
}
....
}

Am I misunderstanding "nested class" concept in C#? Would someone share his
light on this?

Many thanks!
 
D

Daniel O'Connell

fh1996 said:
Documents say that "new" class modifier is only used with nested classes.
"new" indicates that the class hides an inherited member of the same name."

public class MyDerivedC : MyBaseC
{
new public void Invoke() {}
}

In this case, new is on a method, not a class. The new modifier on a method
shadows the MyBaseC.Invoke method, be it inheritable or not, with a new
method named MyDerivedC.Invoke.
The only thing I don't understand in the above statement and example is that
it says that "new" only used with nested classes. However the example isn't
nested class; it's an inheritance. An example of nested class should be
something like:

class NestedClass
{
class MyClass
{
public string name;
public int id;

public MyClass ()
{
}

public MyClass (int id, string name)
{
this.id = id;
this.name = name;
}
}
...
}

You would need new in this case, I think, if your class was inheriting from
another class that already had class MyClass nested within it.
 
P

Pete Davis

Don't confuse "class modifier" with "method modifier." They're two different
things. You're using new as a method modifier and the rules are different.

Pete
 

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

Top