7

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 ?

newday
  • 3,842
  • 10
  • 54
  • 79
  • thanks for the bellow answers. another thing I want to clarify is, can C# compiler compile VB.net code? I think that's what they say in the book. Since it is two compilers, I feel it is not compatible. – newday Dec 29 '13 at 16:41
  • I have updated my answer with an example and answered both of your questions. Hope it will be clear after looking at the example. – Adarsh Shah Dec 29 '13 at 18:47

4 Answers4

10

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.

Adarsh Shah
  • 6,755
  • 2
  • 25
  • 39
5

C# and VB.Net are different languages. They have different programs (executables) which are the actual compilers. These compilers compile the high-level code to IL which is what the CLR understands and executes.

The compiler program can either generate an exe or a dll. For asp.net applications the program makes a dll always.

The below link would help you get familiar with the c# compiler command line program:

http://msdn.microsoft.com/en-us/library/2fdbz5xd.aspx

Update:

Q. Can a C# compiler compile vb code?

The answer is: No.

However, for an asp.net website there is a possiblity of having both c# and vb code in a single project. Take a look at the following link to know how that is done:

http://timheuer.com/blog/archive/2007/02/28/14002.aspx

Update:

why it says "multiple code files only works inherently (along with codeSubDirectories) with the web site model" ?

That was the intent of the website model projects: http://msdn.microsoft.com/en-us/library/dd547590(v=vs.110).aspx#scenarios

deostroll
  • 11,661
  • 21
  • 90
  • 161
  • AS mentioned in the given link, do you have any idea that why it says "multiple code files only works inherently (along with codeSubDirectories) with the web site model."? – newday Dec 30 '13 at 15:49
  • Thanks Lot, very interesting reading. Learn lot from this question. Thanks again. – newday Dec 31 '13 at 13:54
5

ASP.Net is a server-side Web application framework designed for Web development to produce dynamic Web pages.

What you are confused is about the compilation of languages, now, C# and VB.Net has it's own compilers. ASP.Net is a framework that could be achieved using languages like C# and all.

So do not get confused with language compilers. Each language will have their respective compilers that will convert the source code to a Intermediate language (IL) code that the Common Language Runtime (CLR) can understand. So a web framework like ASP.net is a framework that provides a mechanism to process the HTTPRequest and HTTPResponse. Now the language compilers will do their part of work of compiling respective programming language to IL code.

From MSDN:

ASP.NET is a unified Web development model that includes the services necessary for you to build enterprise-class Web applications with a minimum of coding. ASP.NET is part of the .NET Framework, and when coding ASP.NET applications you have access to classes in the .NET Framework. You can code your applications in any language compatible with the common language runtime (CLR), including Microsoft Visual Basic and C#. These languages enable you to develop ASP.NET applications that benefit from the common language runtime, type safety, inheritance, and so on.

Update:

Now answering the second doubt that you posted in the commenting section of your question. (Can C# compiler compile VB code)

Actually you cannot. Let us consider C# compiler, what any compiler like C# or VB know's is that you have certain code written in front of you and you need to understand the code and provide an equivalent IL code in this case. This means, a C# compiler has to work with code that has syntax of C# and a VB compiler has to work with code that has syntax of VB. You see a compiler basically needs to know what it is compiling (processing). Now consider some language X has a X compiler, now the compiler will be written in order to just crack and decode the language X.

Now, this does not stop you from writing VB code that talks to C# code or vice versa. That interoperability. See more.

Community
  • 1
  • 1
Nagaraj Tantri
  • 5,172
  • 12
  • 54
  • 78
3

There is a compiler for C# (CSC.Exe) and one for VB.NET (Vbc.exe). These compilers do not generate machine code but compile the respective language to intermediate language (IL). When executing the program (or running the website) on a computer, IL is interpreted and converted into machine code that is executed on the processor.
The compilers that ASP.NET uses are the same that are also used for other .NET programs, e.g. Windows applications.

Markus
  • 20,838
  • 4
  • 31
  • 55