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 ?
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.
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.
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.
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
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.
Related
How I can get c# version from my code on run time? I use .net core. For example I can get framework version with this code:
FrameworkName = Assembly
.GetEntryAssembly()?
.GetCustomAttribute<TargetFrameworkAttribute>()?
.FrameworkName
And I need something similar for C# version.
There is no such thing. The language version is completely lost in the compilation process, as the end result is just compiled IL code.
The fact that you used C# 6 or C# 7 or Visual Basic.NET is not retained in any form in the runtime program, and is only used to emit code.
I know that Java code is compiled into byte-code, that is executed by the JVM.
What is the case with C# ? I have noticed that applications written in C# have the .exe extension what would suggest they are native machine instructions. but is it really so ?
No.
Like Java, C# is compiled to an intermediary language (called MSIL or CIL).
Unlike Java, the IL is stored in EXE files which have enough actual EXE code to show a dialog box asking users to install .Net.
C# compilation is done in these two steps :
1. Conversion from C# to CIL by the C# compiler
2. Conversion from CIL to instructions that the processor can execute.
A component (just in time) performs this compilation at run time from CIL to machine code
What that .exe is supposed to tell you is that the file is executable. C# is compiled into bytecode, just as java is, but .NET wraps this in a CLR executable.
Look here for a more in depth look at CLR executable http://etutorials.org/Programming/.NET+Framework+Essentials/Chapter+2.+The+Common+Language+Runtime/2.2+CLR+Executables/
c# code is compiled to MSIL. it likes java bytecode. msil will be convert to machine isntrctions at runtime.
C# code is compiled to MSIL, MSIL is taken care by .NET CLR
There is also a project that allows compilation of C# to standalone binary executables: CoreRT
Let's say that I have an entire project build in C# and other project build VB and I made a reference to this VB project Because this two projects need to interact between each other. Is this possible? the compiled code of this two project could live together??. Also if i made the same project VB and C# the compiled version are the same??
They both compile into intermediate language (IL), and you can use a VB.NET library with C# and vice versa.
If you want to examine the compiled versions of both languages I recommend you download ILSpy - you can open up any .NET assembly with it. There are a tiny few things that are possible in the one but not in the other, and IL has features not implemented in either :)
.net / clr uses an intermediate language.
also referred as IL.
it is similiar to Java's ByteCode
code program written in c# and vb may differ in a the resulting IL-Code but,
the executional result is the same.
Yes, you can mix projects written in different languages.
There are some subtle differences in how the C# and VB compiler produce code from the source code, but most code will be identical once it's compiled.
One of those subtle differences for example is that local variables in VB are initialised to zero, while local variables in C# are not initialised.
Create empty solution
Right click solution name in solution explorer, add existing project, add VB project
Right click solution name in solution explorer, add existing project, add C# project
Solution Explorer>VB project>"My Project">References>Add... button>Projects>Select the C# project>OK
Now you can access public methods in the other project.
whats the relation(if any) of MASM assembly language and ILASM. Is there a one to one conversion? Im trying to incorporate Quantum GIS into a program Im kinda writing as I go along! I have GIS on my computer, I have RedGate Reflector and it nor the Object Browser of Visual Studio 2008 couldnt open one(of several which I dont have a strong clue to how they behave) of the .dlls in Quantum. I used the MASM assembly editor and "opened" the same dll and it spewed something I didnt expect to necessarily understand in the first place. How can I/can I make a conversion of that same "code" to something I can interact with in ILASM and Im assuming consequently in Csharp? Thanks a ton for reading and all the responses to earlier questions...please bear in mind Im relatively new to programming in Csharp, and even fresher to MASM and ILASM.
MASM deals with the x86 instructions and is platform/processor dependent, while ILASM reffers to the .Net CIL (common intermediary language) instructions which are platform/processor independent. Switching from something specific to something more general is hard to achieve, that's why, AFAIK, there is no converter from MASM to ILASM (inverse, there is!)
IL is a platform independent layer of abstraction over native code. Code written on the .NET platform in C#, VB.NET, or other .NET language all compile down to an assembly .EXE/.DLL containing IL. Typically, the first time the IL code is executed the .NET runtime will run it through NGen, which compiles it once again down to native code and stores the output in a temporary location where it is actually executed. This allows .NET platform code to be deployed to any platform supporting that .NET framework, regardless of the processor or architecture of the system.
As you've seen, Reflector is great for viewing the code in an assembly because IL can easily be previewed in C# or VB.NET form. This is because IL is generally a little higher level instructions and also contain a lot of metadata that native code wouldn't normally have, such as class, method, and variable names.
It's also possible to compile a .NET project directly to native code by setting the Visual Studio project platform or by calling Ngen.exe directly on the assembly. Once done, it's really difficult to make sense of the native code.
Ther is no relationship between MASM assembly language and ILASM. I don't see you have any way to convert native code to IL code. IL can be understood by CLR only while the MASM assembly language is about native machine code. CLR turns the IL into native code in runtime
I have a vb.net solution and I want to add there a new dll files written in c# and use the functionality from the dll, in the code written in vb.net.
I made several uses of it and it seems working all right,
but is it a smart thing to do messing vb.net code with c# like I want to do .
And what a dangers of what I am doing ?
Thank a lot for help .
Your DLL is not a C# DLL, it's a .NET DLL. Once compiled, all you have is IL - doesn't matter what language it came from. Should be no problem, unless you encounter one of the odd edge cases where the DLL's interface includes something that is not supported by Visual Basic. But this would be very much an edge case.
The Common Language Specification, or CLS, defines the subset of .NET features that must be supported by a .NET language, and if your DLL is CLS compliant, then you can use it with no problems. If you are confused about the difference between the CLS, CTS, CLR etc, then I found the coverage of it in this book very helpful, though it is primarily a C# book.
Mark your code as CLS compliant, and then the C# compiler will warn you if you do anything that might cause problems when your DLL is called from another .Net language.
Some quotes from MSDN
To fully interact with other objects
regardless of the language they were
implemented in, objects must expose to
callers only those features that are
common to all the languages they must
interoperate with. For this reason,
the Common Language Specification
(CLS), which is a set of basic
language features needed by many
applications, has been defined.
You can mark assemblies, modules,
types, and members as CLS-compliant using the CLSCompliantAttribute.
Some CLS-compliant language compilers,
such as the C# compiler, enable you to
specify that you intend your code to
be CLS-compliant. These compilers can
check for CLS compliance and let you
know when your code uses functionality
that is not supported by the CLS.
Also, your organisation will now need C# skills as well as Vb.Net skills. You should probably convince yourself that this is OK, and then convince key decision makers.
You can mix VB and C# code in the same project - I have worked on several projects that have mixed them and have no issues.
You language mix seems to be much more isolated - one solution with multiple C# DLLs and vb project(s).
I don't see many issues with that.
One solution was found here:
However, it is possible to use
different languages in a single
project. You may need to write command
line build file to build the project.
In .NET framework SDK, there is one
sample on it. You could access it in
C:\Program Files\Microsoft Visual
Studio
.NET\FrameworkSDK\Samples\Technologies\CrossDevLan
guage.
This sample demonstrates the use
different development languages in a
single project. This sample creates
two assemblies. The first is a library
or DLL assembly that defines a simple
base class written in managed
extensions for C++. The second
assembly is an executable assembly
that defines three derived classes
written in C#, VB, and IL
(Intermediate Language). These types
derive from each other and ultimately
from the base class written in managed
C++. Finally, the executable creates
instances of each of the derived types
and calls a virtual method for each.
The .NET Framework is an environment
where various developers can work
together seamlessly while developing
in their language of choice.
But you can use both VB.NET and C# code inside asp.net application.
You need to create two folders (ex. vbFolder and csFolder) in App_Code folder and write this code in web.config:
<system.web>
<compilation>
<CODESUBDIRECTORIES>
<ADD directoryName="vbFolder" />
<ADD directoryName="csFolder" />
</CODESUBDIRECTORIES>
</compilation>
</system.web>
Good explanation is here.
I think biggest danger is to have a developer to know both languages; while C# and VB.NET are similar because they're bound to .NET framework, they have some peculiarities.
You'll find many good C# programmers and many good VB.NET programmers, but can be a little harder to find a good programmer for both languages
Also, take a look into this article: A Manager’s Retrospective on the C# versus VB.NET decision as it talks about other items to keep in mind, as developer preferences, language features and recruiting.
Both VB.NET & C# are compiled to MSIL (Microsoft Intermediate Language) not native code and this to complete the full compilation to native (machine) on the end user machine via the exist .NET frame work which is on the end user machine so if it was .NET for operating system x your program should work fine for operating system x and if it was operating system y your application should work fine with OS y, and this is the solution which .NET technology comes with to let the .NET applications operating system in-Dependant.
also there is a COM Marshaler service to support old component (controls) to work with .NET applications so for example you can invoke vb6 control (*.ocx) in C# windows application.
and this is great integration between Microsoft technologies and techniques.
and no need to have developer good in both VB.NET and C#, but any way if you need one, I am here :)
but the question is why I am in both?
this just because I deliver training, So I thought to expand my abilities and I was surprised that they both very near except the syntax.