views:

445

answers:

7

Recently I was talking with a friend of mine who had started a C++ class a couple months ago (his first exposure to programming). We got onto the topic of C# and .NET generally, and he made the point to me that he felt it was 'doomed' for all of the commonly-cited issues (low speed, breakable bytecode, etc). I agreed with him on all those issues, but I held back in saying it was doomed, only because I felt that, in time, languages like C# could instead become native code (if Microsoft so chose to change the implementation of .NET from a bytecode, JIT runtime environemnent to one which compiles directly to native code like your C++ program does).

My question is, am I out to lunch here? I mean, it may take a lot of work (and may break too many things), but there isn't some type of magical barrier which prevents C# code from being compiled natively (if one wanted to do it), right? There was a time where C++ was considered a very high-level language (which it still is, but not as much as in the past) yet now it's the bedrock (along with C) for Microsoft's native APIs. The idea that .NET could one day be on the same level as C++ in that respect seems only to be a matter of time and effort to me, not some fundamental flaw in the design of the language.

EDIT: I should add that if native compilation of .NET is possible, why does Microsoft choose not to go that route? Why have they chosen the JIT bytecode path?

A: 

C# could be natively compiled but it is unlikely the base class library will ever go there. On the flip side, I really don't see much advantage to moving beyond JIT.

Andrew Robinson
+1  A: 

Are you suggesting that the fact that C# is managed code is a design flaw??

BFree
Not so much the managed part but more of the bytecode part. The (permanent) vulnerability to easy decompilation (even with obfuscation) seems to be a huge barrier to its future intake by big software vendors.
GRB
future intake.... ? present intake seems pretty significant.
Andrew Robinson
You really should stop using the term bytecode. It does not apply to C#. And decompilation is a problem for all languages, the best defense is patents and licenses.
Samuel
+26  A: 

Java uses bytecode. C#, while it uses IL as an intermediate step, has always compiled to native code. IL is never directly interpreted for execution as Java bytecode is. You can even pre-compile the IL before distribution, if you really want to (hint: performance is normally better in the long run if you don't).

The idea that C# is slow is laughable. Some of the winforms components are slow, but if you know what you're doing C# itself is a very speedy language. In this day and age it generally comes down to the algorithm anyway; language choice won't help you if you implement a bad bubble sort. If C# helps you use more efficient algorithms from a higher level (and in my experience it generally does) that will trump any of the other speed concerns.


Based on your edit, I also want to explain the (typical) compilation path again.

C# is compiled to IL. This IL is distributed to local machines. A user runs the program, and that program is then JIT-compiled to native code for that machine once. The next time the user runs the program on that machine they're running a fully-native app. There is also a JIT optimizer that can muddy things a bit, but that's the general picture.

The reason you do it this way is to allow individual machines to make compile-time optimizations appropriate to that machine. You end up with faster code on average than if you distributed the same fully-compiled app to everyone.


Regarding decompilation:

The first thing to note is that you can pre-compile to native code before distribution if you really want to. At this point you're close to the same level as if you had distributed a native app. However, that won't stop a determined individual.

It also largely misunderstands the economics at play. Yes, someone might perhaps reverse-engineer your work. But this assumes that all the value of the app is in the technology. It's very common for a programmer to over-value the code, and undervalue the execution of the product: interface design, marketing, connecting with users, and on-going innovation. If you do all of that right, a little extra competition will help you as much as it hurts by building up demand in your market. If you do it wrong, hiding your algorithm won't save you.

If you're more worried about your app showing up on warez sites, you're even more misguided. It'll show up there anyway. A much better strategy is to engage those users.


At the moment, the biggest impediment to adoption (imo) is that the framework redistributable has become mammoth in size. Hopefully they'll address that in a relatively near release.

Joel Coehoorn
I always get a little chuckle out of people that have no clue how far apart C# and Java are.
Samuel
RE: the second paragraph - well said sir
annakata
But then why can I still throw .NET programs into the .NET Reflector and get the source code out of it?Point being, the fact remains that decompilation remains a risk. Yes C++ is decompilable too, but the level of difficulty is worlds apart, and I think this is why you won't see .NET Photoshop.
GRB
Decompilation should not be your only defense against IP theft. It would be the weakest you could have.
Samuel
And as for still being able to throw them into Reflector; after it has been JIT compiled, it doesn't replace the executable with the compiled version it stores it in another location.
Samuel
Y'know, it's funny but my total accrued rep for this question from _votes_ is now -2, because I'd reached the daily cap before making this post :) Funny how that works sometimes :)
Joel Coehoorn
A: 

It certainly could, but the real question is why? I mean, sure, it can be slow(er), but most of the time any major differences in performance come down to design problems (wrong algorithms, thread contention, hogging resources, etc.) rather than issues with the language. As for the "breakable" bytecode, it doesn't really seem to be a huge concern of most companies, considering adoption rates.

What it really comes down to is, what's the best tool for the job? For some, it's C++; for others, Java; for others, C#, or Python, or Erlang.

Adam Jaskiewicz
+1  A: 

C# can be natively compiled using tool such as NGEN, and the MONO (open source .net framework) team has developed full AOT (ahead of time) compilation which allows c# to run on the IPhone. However, full compilation is culbersome because it destroys cross-platform compatibility, and some machine-specific optimizations cannot be done. However, it is also important to note that .net is not an interpreted language, but a JIT (just in time) compiled language, which means it runs natively on the machine.

Razispio
+1  A: 

dude, fyi, you can always compile your c# assemblies into native image using ngen.exe

and are you suggesting .net is flawed design? it was .net which brought back ms back into the game from their crappy vb 5, vb 6, com days. it was one of their biggest bets

java does the same stuff - so are you suggesting java too is a mistake?

reg. big vendors - please note .net has been hugely hugely successful across companies of all sizes (except for those open source guys - nothing wrong with that). all these companies have made significant amount of investments into the .net framework.

and to compare c# speed with c++ is a crazy idea according to me. does c++ give u managed environment along with a world class powerful framework?

and you can always obfuscate your assemblies if you are so paranoid about decompilation

its not about c++ v/s c#, managed v/s unmanaged. both are equally good and equally powerful in their own domains

Raj
A: 

Doomed? Because of supposed performance issues? How about comparing the price of:

  • programmer's hour
  • hardware components

If you have performance issues with applications, it's much cheaper to just buy yourself better hardware, compared to the benefits you loose in switching from a higher-abstraction language to a lower one (and I don't have anything against C++, I've been a C++ developer for a long time).

How about comparing maintenance problems when trying to find memory leaks in C++ code compared to garbage-collected C# code?

"Hardware is Cheap, Programmers are Expensive": http://www.codinghorror.com/blog/archives/001198.html

Igor Brejc
That was his word, not mine ;)
GRB
Well, you can quote me next time you talk to him ;)
Igor Brejc