Trying to understand .NET

来源:百度文库 编辑:神马文学网 时间:2024/07/08 07:27:01
Greetings Everyone,
I‘ve trying to teach myself c++ programming from books, online tutorials and references.  I‘ve read a good deal of stuff and have some basic understanding of what is going on, such things as what are classes, basic built in types, templates, libraries,operators, loops and if statements... etc ( Though I may not understand how to use them all effectively yet. )
It was recommended to me that I should download the C++ Express Edition, that it was a really good set-up for windows programming.  I did.  I love alot of the features that brings to the table.  It seems like a really nice IDE.....
But now I find myself scratching my head over this .NET framework, the common language runtime, just in time compilation, managed assemblies and a whole host of other concepts that were not mentioned at all in every c++ reference that I‘ve read prior to the past couple of weeks.  I‘m having a bit of hard time getting my head around all of this new stuff because I have so many questions that I can not find a definitive answer for, even on Microsofts own website.
There‘s tons of marketing hype easily available but no basic reference as to what indeed the .NET platform is all about.  There‘s tons of class references on the msdn2 domain, but some very basic questions go unaswered for me.
1) Why garbage collection?
2) Why compilation to byte code?
3) Why JIT compilation?
All this is starting to sound alot like Java...
4) Can I turn all this stuff off and still use the .NET framework?
5) Can I compile all the library dependancies into one executable file such that I don‘t have to distribute separate runtimes like Visual Basic?
6) Is there a performance hit from using the .NET framework that one does not have coding to the winapi? ...if so, where is it most likely to creep in and are there ways to limit this performance hit?
I love the new form designer. (what took you so long Microsoft?)  And you can‘t argue with the elegance of the .NET platform‘s class library, but I feel like Microsoft could have just as easily given us a library that elegant while still accessing the native api....Borland did it.
7) Can I use the visual components of the windows controls without using the .NET framework or is the form designer only functional using .NET?
Hopefully someone can straighten me out.
TIA,
Mark
Firstly, welcome.  Secondly the forums are a good place to get answers to your questions however many poster will skip questions like yours because you are asking either broad or numerous questions.  Try to whittle your questions down to a single one that you can ask concisely and you‘ll find you get better responses.
Next, are you trying to learn C++ or .NET?  If you are trying to learn C++ then stick with the Win32 API and MFC.  Avoid the .NET stuff until you have a firm grasp of C++.  If you want to learn .NET then I‘d recommend switching to C# and learning it instead.  C++ is a great language but it is really a challenging language to learn.  Add to that all the non-standards compliant stuff added to support .NET and it just gets ugly.  I‘d recommend learning .NET using C# (which is a mix of Java and C++) and then once you have the hang of .NET go back and pick up C++.  It‘ll be easier to make the transition.  To answer almost all your questions you should invest in a good book on .NET.  There are many out on the market.  I prefer to ignore recommendations and simply go to my local bookstore and thumb through the books until I find one I really like.  Any book on C# will also discuss .NET so you‘ll kill 2 birds with 1 stone.  Unfortunately most C++ books do not cover .NET so you‘ll have to buy at least 2 books for that.  Also try to limit yourself (at least initially) to either the Windows or web platforms.  I would recommend Windows as it is an easier environment to learn in.  Once you get the hang of it then expand your skills to the other environment.
Now to briefly answer your questions (get a good .NET book for the details).
1) Garbage collection frees up unused objects in the system to provide more memory for apps to run in.  In C++ (when using dynamic memory) the memory must be explicitly freed by the programmer otherwise it will hang around (even if it is never used) until the app terminates.  This results in a lot of wasted resources.  Furthermore referencing an object once it has been freed will cause a crash.  .NET alleviates most of this by ensuring that an object won‘t be freed until it is no longer needed (although this isn‘t 100% guaranteed).
2) Similar to Java .NET compiles to MSIL.  This permits you to compile your app once and then run it on any number of platforms that support .NET.  In C++ and most other compiled languages you specify at compile time what platform to target.  This means that if you want to target 4 platforms (x86, PPC, ARM and MIPS for example) then you‘d have to compile 4 times.  Normally the code doesn‘t change at all so the only difference is the final binary.  MSIL eliminates the platform issues.
3) Because of MSIL.  MSIL is not machine runnable so it has to be converted (JITting) to the machine-specific code when it is run.  This does have a noticeable impact on performance but it only occurs the first time the code is run during this session.  There are a variety of tools available to measure and optimize this but in reality I haven‘t experience much of an issue in performance with JITted code.  Because it occurs at odd intervals and only happens on a method level the overhead is really low.
4) No you can‘t.  You can get rid of MSIL by using NGen but NGen introduces many more issues.  Even then you aren‘t guaranteed that MSIL won‘t be used.  There is an article in MSDN Magazine for May 2006 about NGen.
5) The only dependencies you‘ll have is the ones you create yourself.  The .NET framework will be installed on most Windows systems already and so all the .NET framework including the compilers is already taken care of.  You only need to install your binary and any dependencies you create (such as to custom business objects).  As you learn .NET you‘ll generally only have a single binary but as you start working on real projects you‘ll break up your solution into several projects where each project is a separate binary.  As for VB the .NET framework already handles all the Microsoft-provided code.
6) Everything is a matter of tradeoffs.  The .NET runtime does have overhead when it starts, when garbage collection runs, interoping with unmanaged code, JITting, security checks and many other places.  There are ways to reduce the overhead but the reality is that you‘ll find that performance issues are generally in your algorithms and code and not in the framework itself.  Proper code design will remedy many issues.  However unmanaged code has issues itself.  You have to write a lot more code to accomplish something in unmanaged code so every line of code you write could become a performance issue for you.  With .NET the experts at MS have written and optimized the code for you so you don‘t have to worry about it as much.  Interoping with unmanaged code and COM is really slow but fortunately you can limit the places where this happens.  On a positive note however is the fact that due to the JIT process the running code is explicited targeted at the machine running it and not on a "model" machine at compile time.  Imagine for a second that you compile your app for the Pentium computer and ship it.  Later the P10 comes out and provides new operations that the compiler can use to really speed up code.  Unless you recompile your app it‘ll still be using the Pentium code.  However through JIT when MS releases a version of the JIT optimized for P10 then suddenly your code runs better as well even though it didn‘t even exist when you originally wrote your program.  Also note that MS continues to update the framework and optimize the performance.  Because of MSIL you get these updates for free without recompiling.
7)  You can not use the WinForm controls without .NET.  However many of the WinForm controls are simple wrappers around the existing Win32 controls or are available to unmanaged code through third-party libraries.  So if you want to use unmanaged code then use the appropriate Win32 version.  However you‘ll find that the .NET versions are easier to use, more reliable and look nicer in general.
In a nutshell I think you‘re worried about performance and trying to eliminate the .NET framework before you‘ve even had a chance to play around with it.  The general rule is write your code and get it working first and then optimize.  I think you‘ll find that when it comes to performance you won‘t be able to tell the difference.
Good luck,
Michael Taylor - 4/18/06