Can a C # compiler compile VB.Net code?

I found the following in an ASP.NET book. I study and am interested in the following content.

A second important advantage of the IL architecture is that it allows the language to be neutral. To a large extent, the choice of language is no longer dictated by the capabilities of any language over another, but rather their preferences of the developer or tam. You can even mix the language in one application. A class written in C # can be obtained from the VB2005 class, and an exception thrown in the C # method method will be detected in the VB @ 005 method.

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

Update: (Another request)

Can a C # compiler compile VB.Net code?

+6
source share
4 answers

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

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

Answer 1: No, this is not possible, and you will see it in the example below. It gives an error if you try to do this because it is looking for C # syntax.

Question 2: I think what they say in the book. Since these are two compilers, I believe that it is incompatible

Answer 2: it does not say that you can compile VB code using C #, but it says that you can mix languages โ€‹โ€‹in one application, as in the example below, and is still able to compile C # and VB (using them compilers).

The following is an 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 the same project, as it ignores the vb file if its C # project is at build time.

Solution structure

The content of ClassCSharp.cs:

namespace ClassLibraryCSharp { public abstract class ClassCSharp { public int MyProperty { get; set; } protected abstract void Test(); } } 

The contents of the ClassVBInCSharp.vb class in C # ClassLibrary. See how I can inherit a C # class, as well as 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 

The following are the commands that I executed:

 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. The VB compiler is used to compile the 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 the C # compiler to compile the vb class, it throws an error as it searches for C # syntax.

+8
source

C # and VB.Net are different languages. They have different programs (executables) that are actual compilers. These compilers compile high-level code in IL, which both understand and execute the CLR.

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

The following link will help you familiarize yourself 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?

Answer: No.

However, for asp.net, it is possible to have both C # code and vb code in the same project. Take a look at the following link to learn how to do this:

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

Update:

why does he say that "several code files work only in their essence (together with codeSubDirectories) with the website model"?

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

+5
source

ASP.Net is a server-side web application platform designed for web development to create dynamic web pages.

What confuses you is about compiling languages, now C # and VB.Net have their own compilers. ASP.Net is the foundation that can be achieved using languages โ€‹โ€‹like C # and that's it.

Therefore, do not confuse with language compilers. Each language will have its own corresponding compilers that convert the source code into intermediate language (IL) code that the CLR language can understand. Thus, a web infrastructure such as ASP.net is a framework that provides a mechanism for handling HTTPRequest and HTTPResponse. Now the language compilers will do their part of compiling the corresponding programming language into IL code.

From MSDN :

ASP.NET is a single web development model that includes the services needed to create enterprise-class web applications with a minimum of coding. ASP.NET is part of the .NET Framework, and when coding for ASP.NET applications you have access to classes in .NET. Framework You can code your applications in any compatible language with a common execution language (CLR), including Microsoft Visual Basic and C #. These languages โ€‹โ€‹allow you to develop ASP.NET applications that benefit from the common language runtime, enter security, inheritance, etc.

Update:

Now answer the second doubt that you posted in the comment section of your question. (Can the C # compiler compiles VB code)

Actually you cannot . Let's look at the C # compiler, that any compiler, such as C # or VB, knows that you have certain code written in front of you, and you need to understand the code and provide equivalent IL code in this case. This means that the C # compiler must work with code that has the C # syntax, and the VB compiler must work with code that has the VB syntax. You see, the compiler basically needs to know what it compiles (processes). Now consider some language X has an X compiler, now the compiler will be written to simply crack and decode the language X.

Now this does not stop you from writing VB code that speaks with C # code or vice versa. This compatibility. See more details.

+5
source

There is a compiler for C # (CSC.Exe) and one for VB.NET (Vbc.exe). These compilers do not generate machine code, but rather compile the corresponding language into an intermediate language (IL). When executing a program (or launching a website) on a computer, IL is interpreted and converted to machine code that runs on the processor.
The compilers used by ASP.NET are the same as for other .NET programs. Windows applications.

+3
source

All Articles