A Tricky Interview Qn – VB.NET DLL & C# Application Interoperability

Recently I have been asked about a question in 2 interviews.

There is a VB.NET Library which has a class having the following methods

[code lang=”vb”]

Public Class Class1

Public Sub SUM(ByVal a As Integer, ByVal b As Integer)

End Sub

Public Sub SUm(ByVal a As Integer, ByVal b As Integer)

End Sub

Public Sub Sum(ByVal a As Integer, ByVal b As Integer)

End Sub

End Class


Developer has compiled it and Add reference to it in a C# Console Application.

How the developer can use all these methods in C# ?

The Concept in question was C# is case sensitive and VB.NET is case insensitive. Since VB.NET is case insensitive it can have method signature in any of the format given above( an assumption). So the developer has compiled and trying to use this VB.NET DLL in C# Application. What would be the result?.

Simply I too was confused, I also tried to logically think in the way “C# is case sensitive and VB.NET is case insensitive”, but the reality was different. I forgot to conside a fact about IL or MSIL(Microsoft Intermediate Language).

The Real FACT

VB.NET Library is not going to compile, since VB.NET compiler does not allow having methods of same name and same signature inside a class. Even though it’s Case Insensitive, the compiler will not compile and show a compilation error.

Why it will not compile? It should compile right, since it’s Case Insensitive, it should be able to handle same signature methods, but CASING is different. Right?

We forgot a point to think here. VB.NET Compiles in to MSIL, every .NET language compiles the application to MSIL. The MSIL is case sensitive, which means that VB.NET compiler must convert the lines at compile time to match the correct casing for the orginal command at the IL level.

A side effect of not being case sensitive is that at times, the compile may not match casing correctlu to the original declaration of a function, resulting in incorrect behaviour.

For Example Consider the following code snippet.
[code lang=”vb”]

Class MyClass
Overloads Function toString(ByVal Format As String) As String
Return "This is My Formatted String"
End Function
Overloads Overrides Function toString(ByVal Format As String) As String
Return "This is My Simple String"
End Function
Module App
Sub Main()
Dim objMyClass As Object = new MyClass()


End Sub
End Module

This code has strange results in VB.NET. The VB.NET compiler cases the toString method incorrectly to match the first definition of toString in MyClass class(with lower case T), rather than matching both definitions to the one inherited from System.Object(with capital T).
The end result is that toString does not override the ToString method in the base class, and the toString method is never called.

This example explains the complexity of having case-insensitive language in real world.

What happens if you add two public functions in C# with same name but cased differently? VB.NET cannot use the function, and complaints that it cannot resolve the function. For example, a Banking class written in C# has a MakeDeposit, and a makeDeposit method. You can reference the DLL in VB and you can create and instance of Banking class, but if your try to use either MakeDeposit or makeDeposit, the compiler complains that it cannot figure out which one to use. To prevent this you could ask compiler to test for Common Language Specification(CLS) compliance. The CLS describes what is legal to make public in order to be compatible with other languages. To tell the compiler to check for CLS compliance add following line of code to the AssemblyInfo.cs file in your C# project.

[assembly: System.CLSCompliant(true)]

(Taken from O’Reilly – C# & VB.NET Conversion Pocket Reference Guide) – Courtesy

Final Conclusion

It is not possible to compile the VB.NET class with methods having different case, but same name and signature. It’s allowed to have only one method with same signature and name.

Quiet Tricky right. If we remember about MSIL / .NET Core Basics we can easily answer such questions.

Have Fun!!!. If you like this post, share it or leave your comments and ideas.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.