Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

11 August 2012

Can we access C#.NET same method names with different case sensitive in vb.net project

In this Article we will see, can we access C#.NET same method names with different case sensitive in vb.net project.

If we define same method names like Add and add (differs from case sensitive),in C#.NET it will taken as two different methods but where as in VB.NET we will get compile time error as "Add/add has multiple definitions with identical signatures".

In the below example we will create C# class library project which has Add and add methods and one subtract method.

C# Class Library Project
namespace CalcLibrary
{
    public class Calc
    {
        public int Add(int a, int b)
        {
            return a + b + 10;
        }
        public int add(int a, int b)
        {
            return a + b + 20;
        }
        public int subtract(int a, int b)
        {
            return a - b;
        }
    }

}



Build the the project, we will get CalcLibrary.dll in bin folder.

 Now create a VB.NET console application and add reference CalcLibrary.dll to Console Application.In the Below example we will see what happens when we try to access add/Add method from C# Class library project.

VB.NET Console Application
Public Class CalcClient
    Public Shared Sub Main()
        Dim obj As New Calc
        Dim resultPlus As Integer = obj.add(1, 2) 'Compile Error as 'Add' is ambiguous because multiple kinds of members with this name exist in class 'CalcLibrary.Calc'
        Dim resultMinus As Integer = obj.subtract(20, 10)
    End Sub

End Class



From the intellisense,if you can observe you will not get add/Add methods but you can get subtract method in intellisense because it has single method defination.So when we try to  explicitly call add/Add method we will get compile time error as "'Add' is ambiguous because multiple kinds of members with this name exist in class 'CalcLibrary.Calc'"


0 Comments:

Post a Comment