Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can C# compiler compile a VB.Net code?

I found following on a ASP.NET book. I am learning and curious about following content.

The second major advantage of an IL architecture is that it enables the Framework to be language neutral. To a large degree, language choice is no longer dictated by the capabilities of none language over another but rather by their preferences of the developer or the tam. You can even mix language in a single applications. A class written in C# can be derived from a VB2005 class, and an exception thrown in a C# method van be caught in a VB@005 method.

My question is , does ASP.NET use same compiler to compile VB.net and C#?

Update: (Another query)

Can C# compiler compile a VB.Net code ?

like image 415
newday Avatar asked Dec 29 '13 15:12

newday


1 Answers

They have separate compilers (csc.exe for C# and vbc.exe for VB.Net) but they both get compiled into IL and then at run-time JIT compiles it into machine code.

Question 1 : can C# compiler compile VB.net code?

Answer 1: No it can't and you will see that in the below example. It gives an error if you try to do that as it looks for C# syntax.

Question 2: I think that's what they say in the book. Since it is two compilers, I feel it is not compatible

Answer 2: It doesn't say that you can compile VB code using C# but it says that you can mix languages in a single application like I did in the example below and still able to compile C# and VB (using their compilers).

See below example to understand how it works. I created a solution with a C# project with a C# class and a VB project with a VB class. You should not mix C# and VB classes in same project as it will ignore the vb file if its a C# project during build.

Solution Structure

Content of ClassCSharp.cs:

namespace ClassLibraryCSharp
{
    public abstract class ClassCSharp
    {
        public int MyProperty { get; set; }

        protected abstract void Test();
    }
}

Content of ClassVBInCSharp.vb in C# ClassLibrary. See how I can inherit from a C# class and also access its properties and override the method.

Namespace ClassLibraryVB
    Public Class ClassVBInCSharp
        Inherits ClassCSharp
        Property Test2 As Integer

        Protected Overrides Sub Test()
            Test2 = MyBase.MyProperty
        End Sub
    End Class
End Namespace

See below commands I ran:

vbc.exe /reference:"ClassLibraryCSharp.dll" /target:library /out:"ClassLibraryCSharpVbVersion.dll" "ClassVBInCSharp.vb"
Microsoft (R) Visual Basic Compiler version 12.0.20806.33440
Copyright (c) Microsoft Corporation.  All rights reserved.

See above VB Compiler is used to compile vb class.

csc.exe /reference:"ClassLibraryCSharp.dll" /target:library /out:"ClassLibraryCSharpVersion.dll" "ClassVBInCSharp.vb"
Microsoft (R) Visual C# Compiler version 4.0.30319.33440 for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

ClassLibrary1\ClassVBInCSharp.vb(1,1): error CS
0116: A namespace cannot directly contain members such as fields or methods

See above if I try to use C# Compiler to compile vb class it throws an error as its looking for C# syntax.

like image 138
Adarsh Shah Avatar answered Oct 08 '22 18:10

Adarsh Shah