Difference between C#'s "new" keyword and VB.NET's "shadows" keyword

  • Thread starter Thread starter Dot net work
  • Start date Start date
D

Dot net work

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#.

I'm sure there's an easy way around this, but I am a VB.NET
programmer, and not a C# programmer.

Please C# experts, can you advise me...

TIA,
-dnw.
 
Dot said:
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#.

I'm sure there's an easy way around this, but I am a VB.NET
programmer, and not a C# programmer.

Even though shadows seems to hide it, it isn't hidden. Everyone can reach
the base class methods through reflection.

FB
 
Hello Frans,
Even though shadows seems to hide it, it isn't hidden. Everyone
can reach
the base class methods through reflection.

Through reflection you can also reach private members...


But you are right.
"shadows" is only a feature of Intellisence of VB.NET!!!

If you use this class in C#, you can see all members!!!


Here is a small example:

<code_vb>
Public Class Test1
Public Function F1()
End Function

Public Function F1(ByVal a As Boolean) As Long
End Function

Public Function F1(ByVal b As Int16, ByVal c As Long) As Boolean
End Function
End Class

Public Class Test2
Inherits Test1

Public Shadows Function F1(ByVal d As Double) As Double
End Function

End Class
</code_vb>


In VB.NET you will see only the Test2.F1 (via Intellisense)!
But you can also call all methods from Test1-class !!! You put the
correct parameters and it will call the method (of course, it is not
supported by intellinsense!).


Also in C#, here you will see all 4 methods via intellisense.

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
 
Indeed - VB.NET seems kind of special in this case.
I like this extra functionality for the shadows keyword inside VB.NET.
("shadows" and "new" are not the same.)

I like the ability to hide base class methods, because it provides
less confusion/ambiguity for the calling code.

I tried a simple experiment in VB.NET, and the shadows keyword makes
it impossible to access base class members through simple use of
object.method calling syntax. The editor highlights the problem, and
if you go ahead and run the code anyway, it just calls the inherited
method, and not the base class method. The calling code knows nothing
about the base class method/s. (Naturally, we are talking about
methods whose names are the same, but differ in signature.)

-dnw.
 
Hello Dot net work;
Indeed - VB.NET seems kind of special in this case.
I like this extra functionality for the shadows keyword inside VB.NET.
("shadows" and "new" are not the same.)

Indeed, "shadows" and "new" IS tha same (at least from CLR perspective),
but VB.NET does some "special treatment" if it is a VB.NET-assembly...
I like the ability to hide base class methods, because it provides
less confusion/ambiguity for the calling code.

I tried a simple experiment in VB.NET, and the shadows keyword makes
it impossible to access base class members through simple use of
object.method calling syntax. The editor highlights the problem, and
if you go ahead and run the code anyway, it just calls the inherited
method, and not the base class method. The calling code knows nothing
about the base class method/s. (Naturally, we are talking about
methods whose names are the same, but differ in signature.)

Yes, but this is ONLY true for VB.NET. If you use this assembly in an other
languange (like C#) you can call all methodes...

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
 
Back
Top