Name hiding should be allowed in some cases

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
P

puzzlecracker

I encountered this problem while having code in the following form:


public class Base:IFormattable
{

public string ToString(string format, IFormatProvider
formatProvider)
{
// Do some stuff here

}
public override String ToString() { return ToString(null,
null); }

}

public class Derived1:Base
{

public override String ToString() { return ToString(null,
null); }
public new string ToString(string format, IFormatProvider
formatProvider)
{ }

}

public class Derived2:Base
{

public override String ToString() { return ToString(null,
null); }
public new string ToString(string format, IFormatProvider
formatProvider)
{ }

}

Discussion: The mandatory usage of the keywords 'new' and 'override'
not only befuddle me, but also appear as if they add more mess to the
code. I think the new shouldn't be required in this case just because
we have two ToString member methods. WHY WHY WHY

What's the point to have us programmers write in this way?

Thanks
 
This is part of the design philosophy of C# - you should be able to know the
purpose of a member from its declaration. That is, by glancing at the member
declaration, you should know if it overrides a base class member, or hides a
base class member. Compare this to C++ or Java where you have to compare the
derived class members to the base class members to know the same information.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++
 
This is part of the design philosophy of C# - you should be able to know the
purpose of a member from its declaration.  That is, by glancing at the member
declaration, you should know if it overrides a base class member, or hides a
base class member.  Compare this to C++ or Java where you have to compare the
derived class members to the base class members to know the same information.
--http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++

Old habits die hard, as I am coding in both languages (c++ and c#) on
almost daily bases for living....

I need a cheat sheet of all the inheritance/polymorphism rules of
csharp, as I always get confused......


Thanks...
 
Back
Top