0

.NET has C# and VB* as (relatively) high-level languages, both of which compile to the .NET CLI intermediate code (.NET's equivalent of Java's "bytecode"), and not to native machine code. There is also C++/CLI, which lets you write code for the .NET CLI intermediate code.

These can be familiar with each other the same way C#/VB projects can, with C++/CLI code able to use C#-defined elements (classes, methods, properties) with keywords like gcnew and notation ^, and C#/VB code. C++/CLI allows working with pointers, memory allocation, and getting and setting the actual bytes in memory (by pointers). However, these are things that can be done with C# (and VB?) with the unsafe keyword.

Is there a difference between the two ways? Are there things you can achieve with one but not the other? Is there a performance difference (consider both compile to intermediate .NET CLI code and not machine code)?

* when I say C#/VB, I only know the C# part so I may by wrong. I'm not familiar with VB.

Thanks in advance!

uvero
  • 81
  • 1
  • 1
  • 9
  • minor note: most things that you can do in C# with the `unsafe` keyword, you can also now do *without* the `unsafe` keyword via spans and ref-returns/locals - in many cases, with the JIT still eliding any bounds checks (if the JIT is sure from the context that you aren't exceeding it) – Marc Gravell Jun 02 '20 at 09:16
  • 1
    Should not have `c++` tag, since C++/CLI is not C++. – Eljay Jun 02 '20 at 12:57

1 Answers1

1

The primary reason for C++/CLI is to have an easy way to interface to native c++ libraries, this is the most important difference.

In c++/cli there is a possibility to compile native code using the native VS C++ compiler. This can provide a speedup since it is possible to use lower level techniques, including SIMD, and since the compiler in general is better at optimization.Source.

If both compile to MSIL your performance will in both cases mostly depend on the JIT compiler. But there might be some minor difference I'm not aware of.

JonasH
  • 28,608
  • 2
  • 10
  • 23