1.396 Aufrufe

Veröffentlicht am

0 Kommentare
3 Gefällt mir
  • Als Erste(r) kommentieren

Keine Downloads
Aufrufe insgesamt
Auf SlideShare
Aus Einbettungen
Anzahl an Einbettungen
Gefällt mir
Einbettungen 0
Keine Einbettungen

Keine Notizen für die Folie
  • Sample Dialog: Speaker Instructions: You should introduce yourself on this slide. Briefly address the topic of the evening and mention the length of the presentation. You can also discuss the nature of your organization (user group, etc.) while on this slide.
  • Sample Dialog: The .NET Framework is a new computing platform developed by Microsoft. It can be somewhat difficult to describe, because it plays a number of roles. It is a software development environment and SDK. It is a runtime environment for executable code, and it is a platform with many features of an OS. C# and other languages can be used to target the .NET Framework, and the .NET Framework as a platform is designed to target the Internet.
  • Sample Dialog: The common Internet software of today includes a client which communicates with a web server. The client is most likely running a browser or some other standard client such as an FTP client. The Server is running any of a number of server products to serve the client. Moving forward, the lines of communication will open up. Second and third tiers will communicate directly with clients. Clients will communicate with each other in a pier-to-pier relationship, and servers across realm and company boundaries will also communicate on a pier-to-pier bases. The nature of the communication itself will also expand. At this time most communication is either HTML over HTTP or HTTPS, or it is proprietary. However, moving forward, SOAP and XML will allow for more flexible communication between parties. Traditionally, writing software that runs on the client (typical applications, as well as client applications) is vastly different from writing server or back-end server programs. The tools and languages differ, as well as the libraries and reusable code. This makes programming for the Internet difficult and often inefficient. The .NET Framework is built from the ground up to target each level shown on this slide. Meanwhile the language, libraries, and tools you prefer can be used for client and server tasks. Speaker Instructions: There is a lot of sample dialog on this slide you can pick which points you wish to make. The last sentence is important though.
  • Sample Dialog: Before digging too deeply into what the .NET Framework is and does, I would like to show a little C# code. C# is only one of the languages that you can use to write .NET programs, however it is one of the most popular. Speaker Instructions: Consider bringing up this source code in an editor. Either way, walk through the code. Point out the class definition as well as the main function. Point out that this code has a C-based syntax much like Java or C++. Point out the use of preexisting types such as Application and Form. On the next slide you should compile and run the code, and perhaps briefly explain how it works.
  • Sample Dialog: Now I am going to compile and run the C# program from the last slide. As you can see it is a simple “Hello World” GUI application… Speaker Instructions: You can use Visual Studio or the command line compiler here to run this code. You should explain how the code works. Point out that this is an example of a managed application, and that you will define that term in more detail shortly. The source code for HelloGUI.cs is included as a Visual Studio.NET with this module.
  • Sample Dialog: The .NET Framework is made up of several parts. One of the most important pieces of the framework is the Common Language Runtime or CLR. The CLR is a runtime engine that manages .NET code as it is running. Management includes security management, memory management, thread management and a host of other system services. For this reason all code targeting the .NET Framework (including C# code) is called Managed Code There are a number of language compilers that produce managed executable files. C# is a popular language, but you can use Java, Visual Basic, C++, Cobol, PERL, etc. Speaker Instructions: It is important that the reader understand that what is true of managed code is true across all languages that can be managed. The cross language abilities of the .NET Framework are a big win for programmers. This is why it is helpful to refer to executables and modules as managed executables and managed modules, rather than always saying C# executables (since C# is a specific language).
  • Sample Dialog: You can see here the relationchip between your managed executable, the CLR, and the underlying operating system. The CLR runs on Windows (and other operating systems in the future). Managed code runs on the CLR, and makes use of reuseable object libraries (often managed as well). Legacy or unmanaged software runs along-side managed code. Speaker Instructions: Managed code is not interpreted. This slide can give that impression. It is important to note that this is not the case, and emphasize it later on when we cover JIT compiling.
  • Sample Dialog: What makes a managed executable managed is the fact that it is made up of IL instructions and metadata. IL stands for Intermediate Language, and is a “CPU independent assembly language.” Managed code logic is represented as IL rather than x86 machine language instructions. Metadata is also an important part of managed code. In fact, a managed EXE would not be manageable without its metadata. The metadata describes everything that can’t be described by IL. That is to say that metadata describes everything but the executable logic. This includes class definitions, method definitions, parameter types and return types. Between metadata and IL, the CLR has enough information about your managed code to insure type-safety at all times. Speaker Instructions: The type safety issue is an important one as code modules become increasingly distributed over the Internet. It might be a bit much to attempt to relay this in an hour session.
  • Sample Dialog: So if you look at this diagram, you can see that you start with a C# source file (this could be any .NET language), and then compile it. What the compiler produces is an .EXE file that contains Metadata and IL (always in the same file). This is a managed executable. Note that IL is not C#, and by the time your software is in binary form it has no relationship to its source language. Speaker Instructions: This last point is an important first step in understanding the language agnosticism of the .NET Framework. Language agnosticism is a major improvement over Java. If anybody asks if IL is basically another name for p-code say, “yes and no”. Here are the facts. IL is programming language agnostic, where p-code is typically tied to a language. IL builds on the good parts of p-code, but moves forward. Another difference is that IL is always JIT compiled, and executed in native machine language, where p-code is often interpreted. By the way, just FYI, Sun did not create p-code. P-code has existed long before Java. There were p-code Pascal compilers in the 70’s. (You don’t have to bring this up unless you are pressed by someone who thinks that IL is just a stolen idea).
  • Sample Dialog: One great thing about managed code is that it is NOT interpreted. ALL managed code runs in native machine language, even though it is represented as CPU independent IL. This is possible, because at runtime the CLR Just-In-Time compiles (or JIT compiles) the IL into native code, and then it executes the code. This really does allow for the best of both worlds between code interpretation and the performance of native machine language. Speaker Instructions: JIT compiling is a cool feature, and you should feel comfortable with the concepts behind it before presenting in class. There are often a number of questions on this topic. The tutorial goes into JIT compilation in some detail.
  • Sample Dialog: When a user executes a managed application, the system immediate loads the CLR unto the new processes. The CLR then begins JIT compiling the IL from the executable and executing it. It is not necessary for every IL instruction in an executable to be JIT compiled before it is run. This happens as needed. Once the code is running, it is run directly by the CPU. However, the JIT compiler emits code that insures that your program remains manageable by the CLR. So even though the code is running native, it is always managed code. Speaker Instructions: The notion of managed code running in native machine language can be tough to comprehend. But the point is that it is IL until the point of running. It is the CLR that creates the machine language, so it has all of the control necessary to maintain the managed status of the software.
  • Sample Dialog: One very important aspect that the CLR manages for your .NET code is memory management. All memory allocations come from a Managed Heap. Your code never explicitly frees memory. Once memory is no longer referenced by your software it is eligible for Garbage Collection. Although memory management can be a fun part of computer programming, it is important for managed code to allow the system to manage memory, because moving forward many applications will be comprised of objects developed elsewhere and distributed across the internet. Since we can’t trust these objects to have boundless access to memory, it is important that memory is managed. Additionally, so very painful and common memory bugs are impossible with managed code. Speaker Instructions: Often there are questions about performance related to the Garbage Collector. Here are some facts. Garbage collection takes time but happens rarely Meanwhile, managed heap allocations are MUCH faster than a malloc or new in C++ Memory fragmentation is impossible with the managed heap, so memory is used MUCH more efficiently with the managed heap.
  • Sample Dialog: Garbage collection can happen at any time that your program needs memory. It typically happens in response to a “new” operation. When a garbage collection occurs, garbage objects and memory buffers are removed from the managed heap. In this example, if a garbage collection were to occur during the execution of the Method() method, objects A and D would be cleaned up, because they are not referenced by code, nor are they referenced by objects that are referenced by code. Speaker Instructions: The arrows pointing from one object to the next in the managed heap indicate fields in objects that refer to other objects in the managed heap. The arrows pointing from the source code to the heap indicate reference variables on the stack in your running code, that refer to objects in the managed heap.
  • Sample Dialog: Because managed code is IL and metadata, it is possible for any compiler to be modified to produce managed code. All it needs to do is produce an EXE comprised of IL and Metadata, and now the exe can run with the CLR. There are many languages already in existence or in development that target the .NET Framework. The advantage is that programmers can use their language of choice while being able to use the same tools and code libraries as any other managed code. Speaker Instructions:
  • Sample Dialog: Visual Studio.NET is not part of the .NET Framework, however it is a great development environment for building .NET code. You can use the command-line compilers that come with the .NET Framework to build managed code, however Visual Studio.NET includes useful wizards and tools that the .NET Framework does not ship with. It is important to distinguish between Visual Studio and the .NET Framework. The .NET Framework is a platform (and an SDK). Visual Studio is an Integrated Development Environment that targets the platform that is the .NET Framework. Speaker Instructions: Visual Studio costs money, the .NET Framework is free.
  • Sample Dialog: Speaker Instructions: You should bring up Visual Studio.NET and create a C# windowed project. Drag and drop a button or two onto the form, and then build and run the application. Show the code that was produced by the designer. Point out that this code could be written from scratch, but that Visual Studio makes the development process much easier.
  • Sample Dialog: The CLR is object oriented through and through. So is C#. You will find that you make use of reusable object libraries a lot when writing managed code. The Framework Class Library (or FCL) is a huge object library that ships with the .NET Framework. It has many classes, interfaces, and structures (collectively called “types”), that help you do a number of common and advanced programming tasks. Familiarity with the FCL is critically important for a .NET developer. You will find classes in the FCL for creating and manipulating files, collection classes, windowing and GUI programming, web-sight development and hundreds more. Additionally, the FCL is built for extension, so it is common to write software that derives from classes in the FCL to extend their features. Speaker Instructions: The lines between applications and components will become much blurrier. This important in the evolution of the software industry in general.
  • Sample Dialog: Types in the FCL are arranged in a hierarchy of namespaces. It is helpful to be familiar with the namespaces, so that you can find related types more easily. A types full name includes its namespace. So in your code you may refer to a type named “Form”, however the CLR will always think of the type as System.Windows.Forms.Form, where System.Windows.Forms is the type’s namespace. Types are also published in files called assemblies. The sdk documentation indicates a types namespace and assembly. The namespace should be added to the top of your source module (if you are using the type) with a “using” statement. The assembly for a type should be added to your project references when building the program. Speaker Instructions: Become comfortable with assembly references and the using statement before presenting this slide. You will most likely get questions. The Visual Studio.NET IDE allows you to add references to your project. These references are assemblies, and the appropriate assembly to choose depends on the types that you are using in your sources.
  • Sample Dialog: Speaker Instructions: You should bring up the help topic for System.Windows.Forms.Form and walk through the different parts of the help. You can use the topic in the tutorial as a guideline for what parts to show in the help. The MOST important parts are the namespace and assembly items near the bottom of the help. But it is also useful to point out the sample code and other information in the topic.
  • Sample Dialog: Now that we have discussed the .NET Framework in general, it is good to cover what you can do with it. First of all, you can write typical applications. These are your standard GUI or console applications that run on the users machine. More importantly, however, managed code and the FCL are designed to promote Internet distributed code. This includes web-pages that run as software on the server side. This also includes web-services which are a non GUI server-side object. Web-servers can distribute managed objects to be executed locally on the client. In this respect, managed code can run just about anywhere, and makes for some very interesting application design possibilities. Finally, the CLR can be hosted in any application. This makes it possible to use managed code as the script or macro language for your program. Meanwhile, wherever managed code runs, it is always language agnostic, JIT compiled and executed natively, and it always has full access to the FCL. Speaker Instructions: The win here is that when you get comfortable writing any kind of C# application you can apply those skills to more advanced or vastly different types of applications. No more learning a whole net set of “stuff” just because your application is a different kind of application.
  • Sample Dialog: So now we can take a look at the distributed code slide, and apply our knowledge of managed code. If you are writing a client component, then your managed program is likely to using Windows.Forms for GUI and the FCL classes for communicating with servers. Client code can also be hosted in a browser. If you are writing web server code, then the CLR is hosted in a server product like IIS and you are probably writing Web Forms or Web Services code. Your software is still C# or VB or whatever language you choose, but now it runs with all of the services of a high-scalability web-server. Finally, second tier servers like data-base servers can host managed code. Managed code can be used as stored procedures or just about any logic in an application that hosts the CLR. The benefit is that you get to use tools, languages, and code that you are already comfortable with. Speaker Instructions: This is a repeat of the slide found earlier in the deck. When presenting this slide, you should point to a client box, and describe the kinds of managed code that runs there. Then the web-server, then the second tier. This helps round out the user’s understanding.
  • Sample Dialog: In closing I would like to demo a simple Web Forms application. This application runs on the server side, and is hosted in IIS. Any browser can be used to browse to this application. (It does not need to be running on Windows). Speaker Instructions: You should browse to the Draw.aspx file and show how the application works. Then you should show the source code for the application. Point out that the website is implemented using C# like any other application. If you have time, run through the logic of the code. Point out to the user that the code comes with the tutorial and they can try it themselves. Also point out that Web Forms are an advanced topic and that an entire session is devoted to the topic.
  • Sample Dialog: Speaker Instructions: Open the floor to questions

    1. 1. Introducing the .NET Framework with C# Global Computer Center
    2. 2. Assemblies Assemblies are the physical packaging of the class libraries. These are .dll files, but don't confuse them with Win32 shared libraries. Examples are mscorlib.dll System.dll System.Data.dll Accessibility.dll Note that namespaces are often distributed among several assemblies
    3. 3. Defining the .NET Framework <ul><li>The .NET Framework is </li></ul><ul><ul><li>A software development environment </li></ul></ul><ul><ul><li>A runtime engine for Managed Code </li></ul></ul><ul><ul><li>A platform designed for Internet-Distributed software </li></ul></ul><ul><li>The .NET Framework is an exciting new computing platform </li></ul>
    4. 4. Internet Distributed Software Web Server Client Client Web Server Second-Tier (database or other server) Second-Tier Second-Tier
    5. 5. A First Look at Managed Code using System.Windows.Forms; using System.Drawing; class MyForm:Form{ public static void Main(){ Application.Run(new MyForm()); } protected override void OnPaint(PaintEventArgs e){ e.Graphics.DrawString(&quot;Hello World!&quot;, new Font(&quot;Arial&quot;, 35), Brushes.Blue, 10, 100); } } HelloGUI.cs c:> csc /target:winexe HelloGui.cs
    6. 6. Demo HelloGUI.cs
    7. 7. Managed Code and the CLR <ul><li>The Common Language Runtime (CLR) is a runtime engine </li></ul><ul><ul><li>Manages .NET Code (such as C# applications) </li></ul></ul><ul><ul><li>Provides features such as memory management, thread management, object type safety, security, etc. </li></ul></ul><ul><ul><li>Is a part of the .NET Framework </li></ul></ul><ul><li>Managed code </li></ul><ul><ul><li>Code that targets the CLR </li></ul></ul><ul><ul><li>Any .NET Language, including C#, Visual Basic, C++, Java, Cobol, etc. </li></ul></ul>
    8. 8. The CLR and Managed Code Windows (or other operating oystem) Common Language Runtime (JIT compilation, memory management, etc.) Legacy Software (unmanaged code) Managed Executable Reusable Managed Components
    9. 9. IL and Metadata <ul><li>All Managed executables consist of </li></ul><ul><ul><li>Intermediate Language (IL) instructions </li></ul></ul><ul><ul><li>Metadata </li></ul></ul><ul><li>IL </li></ul><ul><ul><li>CPU independent machine language </li></ul></ul><ul><ul><li>Just-in-time compiled at runtime </li></ul></ul><ul><li>Metadata </li></ul><ul><ul><li>Structured information </li></ul></ul><ul><ul><li>describes programming constructs including </li></ul></ul><ul><ul><ul><li>Classes definitions, field and method definitions, parameter lists, return types, etc. </li></ul></ul></ul>
    10. 10. From Source Code to Managed .exe C# Compiler A Managed Application This Cod is output with Manifest Into a PE (Portably Executable ) File using System.Windows.Forms; using System.Drawing; class MyForm:Form{ public static void Main(){ Application.Run( new MyForm()); } protected override void OnPaint( PaintEventArgs e){ e.Graphics.DrawString( &quot;Hello World!&quot;, new Font(&quot;Arial&quot;,35), Brushes.Blue, 10, 100); SomeSource.cs SomeSources.exe IL Metadata
    11. 11. Just-in-Time Compiling <ul><li>All managed code runs in native machine language </li></ul><ul><li>However, all managed code is made up of IL and metadata </li></ul><ul><li>The CLR JIT-compiles the IL and metadata </li></ul><ul><ul><li>At execution time </li></ul></ul><ul><ul><li>Executed directly by CPU </li></ul></ul><ul><li>Allows for the best of both worlds </li></ul><ul><ul><li>Code management features </li></ul></ul><ul><ul><li>Performance of full-speed execution </li></ul></ul>
    12. 12. Executing a Managed Application Running Process’ Memory JIT Compiler 10010100 10110000 10000000 10111010 11011011 11010111 11000010 01110110 Native Machine Language The CPU executes the JIT- compiled machine code directly At execution time the IL and Metadata are JIT compiled SomeSources.exe IL Metadata
    13. 13. Automatic Memory Management <ul><li>The CLR manages memory for managed code </li></ul><ul><ul><li>All allocations of objects and buffers made from a Managed Heap </li></ul></ul><ul><ul><li>Unused objects and buffers are cleaned up automatically through Garbage Collection </li></ul></ul><ul><li>Some of the worst bugs in software development are not possible with managed code </li></ul><ul><ul><li>Leaked memory or objects </li></ul></ul><ul><ul><li>References to freed or non-existent objects </li></ul></ul><ul><ul><li>Reading of uninitialized variables </li></ul></ul><ul><li>Pointerless environment </li></ul>
    14. 14. Garbage Collection A B E D C The Managed Heap = Object or buffer in memory class MyClass{ void Method(){ Variable v1; Variable v2; do{ . . . Objects A and D will be cleaned up because neither is directly or indirectly referenced by code
    15. 15. Cross Language Support <ul><li>The .NET Framework supports many languages </li></ul><ul><li>Any compiler can be modified to emit managed executables </li></ul><ul><ul><li>IL and metadata </li></ul></ul><ul><li>Languages that target the .NET Framework </li></ul><ul><ul><li>C#, Visual Basic, C++, Java, PERL, COBOL, SmallTalk </li></ul></ul><ul><ul><li>Dozens more existing and on the way </li></ul></ul><ul><li>Regardless of source language, all managed code can </li></ul><ul><ul><li>Use the same tools </li></ul></ul><ul><ul><li>Use the same reusable components </li></ul></ul><ul><ul><li>Take advantage of features of the CLR </li></ul></ul><ul><li>Developers use their language of choice </li></ul>
    16. 16. Visual Studio.NET <ul><li>A Integrated Development Environment </li></ul><ul><ul><li>To develop managed and unmanaged applications </li></ul></ul><ul><ul><li>Supports C#, C++, Visual Basic, JavaScript </li></ul></ul><ul><ul><li>Many useful tools and wizards </li></ul></ul><ul><li>Not part of the .NET Framework </li></ul><ul><ul><li>Not necessary to build or run managed code </li></ul></ul><ul><ul><li>The .NET Framework SDK includes command line compilers </li></ul></ul>
    17. 17. Demo Visual Studio.NET
    18. 18. The Framework Class Library <ul><li>A huge collection of reusable types </li></ul><ul><ul><li>Classes, interfaces, enumerations and structures </li></ul></ul><ul><li>For use by any managed code </li></ul><ul><ul><li>Including code written in any managed programming language </li></ul></ul><ul><li>Types for </li></ul><ul><ul><li>Common tasks such as collections, file IO, memory and thread management </li></ul></ul><ul><ul><li>GUI and window manipulation </li></ul></ul><ul><ul><li>Web form and web service applications </li></ul></ul><ul><li>Totally object oriented toolbox for developers </li></ul><ul><ul><li>Ships as part of the .NET Framework </li></ul></ul>
    19. 19. Using the FCL <ul><li>Types are arranged in a hierarchy of Namespaces </li></ul><ul><li>A type’s full name includes its namespaces </li></ul><ul><ul><li>Form class is actually System.Windows.Forms.Form </li></ul></ul><ul><ul><li>Use using to indicate namespaces in source code </li></ul></ul><ul><li>Assembly references are necessary for many types </li></ul><ul><li>The SDK documentation is critical for using SDK types </li></ul><ul><ul><li>Includes descriptions </li></ul></ul><ul><ul><li>Often includes useful code samples </li></ul></ul><ul><ul><li>Indicates namespaces and assemblies for types </li></ul></ul>
    20. 20. Demo The FCL SDK
    21. 21. Types of Managed Applications <ul><li>Common application </li></ul><ul><ul><li>GUI or windowed applications (Windows.Forms) </li></ul></ul><ul><ul><li>Console applications or command line apps </li></ul></ul><ul><li>Web applications ( </li></ul><ul><ul><li>Web forms applications </li></ul></ul><ul><ul><li>Web services </li></ul></ul><ul><li>Scripted code </li></ul><ul><ul><li>Applications like word processors or database servers can host the CLR </li></ul></ul><ul><ul><li>Use any managed language (C#, VB, etc.) as a script or macro language </li></ul></ul><ul><li>Wherever you use managed code </li></ul><ul><ul><li>Same compilers and tools </li></ul></ul><ul><ul><li>Same FCL </li></ul></ul><ul><ul><li>Same CLR (JIT compiled, object oriented, memory managed, etc.) </li></ul></ul>
    22. 22. Distributing Managed Code Web Server Client Client Web Server Second-Tier (database or other server) Second-Tier Second-Tier
    23. 23. Demo Draw.aspx
    24. 24. Introducing the .NET Framework with C#
    25. 26. Class Library The class library provides a comprehensive set of facilities for application development. Primarily written in C#, it can be used by any language, thanks to the Common Language Specification . The class library is structured into Namespaces, and deployed in shared libraries known as Assemblies. When we speak of the .NET framework , we are primarily referring to this class library.
    26. 27. Namespaces Namespaces are a mechanism for logically grouping similar classes into a hierarchical structure. This prevents naming conflicts. The structure is implemented using dot-separated words. The top level namespace for most of the .NET framework is System . Under the System namespace you'll find the following: System.IO System.Net System.Net.Sockets System.Reflection System.Threading ...and many others. There are other top-level namespaces as well; Accessibility , and Windows are examples. You can also create your own namespaces: just prefix them with the name of your organization. Microsoft.VisualBasic is an example.
    27. 28. Common Language Specification (CLS): 1)The Common Language Specification (CLS) describes a set of features that different languages have in common. The CLS includes a subset of the Common Type System (CTS). 2)The CTS makes available a common set of data types so that compiled code of one language could easily interoperate with compiled code of another language by understanding each others’ data types. 3)The common type system is a rich type system, built into the common language runtime, that supports the types and operations found in most programming languages. The common type system supports the complete implementation of a wide range of programming languages. 4)common type system this is used to communicate with other language. example in vb we have int and in c++ we have long so that in one case they are not compatiable with each other so that CTS palys important role with using System.int32