new class modifier and tested class

  • Thread starter Thread starter fh1996
  • Start date Start date
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!
 
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.
 
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
 
Back
Top