02 Development.ppt

1,121 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

02 Development.ppt

  1. 1. 2. Developing in .NET and C#
  2. 2. Objectives <ul><li>“ Microsoft .NET development is based on an underlying framework of tools and classes. These tools and classes are known as the Framework SDK (Software Development Kit).” </li></ul><ul><li>.NET development options </li></ul><ul><li>Brief summary of C# </li></ul><ul><li>Class-based development </li></ul><ul><li>Component-based development </li></ul>
  3. 3. Part 1 <ul><li>.NET development options… </li></ul>
  4. 4. Recall assemblies <ul><li>1 assembly = 1 or more compiled classes </li></ul><ul><ul><li>.EXE represents an assembly with classes + Main program </li></ul></ul><ul><ul><li>.DLL represents an assembly with classes </li></ul></ul>Development Tools assembly code.vb code.vb code.cs .EXE / .DLL
  5. 5. .NET development <ul><li>There are currently 3 ways to develop assemblies: </li></ul><ul><li>1) .NET Framework SDK </li></ul><ul><ul><ul><li>free (100 MB) </li></ul></ul></ul><ul><ul><ul><li>complete set of command-line tools and docs </li></ul></ul></ul><ul><ul><ul><li>available for Windows NT, 2000, XP Pro, 2003 </li></ul></ul></ul><ul><ul><ul><li>http://msdn.microsoft.com/net </li></ul></ul></ul><ul><ul><ul><li>other platforms? </li></ul></ul></ul><ul><ul><ul><ul><li>FreeBSD / Mac OS X via Rotor (i.e. SSCLI) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Linux via Mono project </li></ul></ul></ul></ul>
  6. 6. Development options, cont'd <ul><li>2) Visual Studio .NET </li></ul><ul><ul><ul><li>5-6 CDs, 192 MB RAM (bare minimum) </li></ul></ul></ul><ul><ul><ul><li>powerful, integrated development environment (IDE) </li></ul></ul></ul><ul><ul><ul><li>one IDE for all: GUI, web-based, web service, DLLs, etc. </li></ul></ul></ul><ul><ul><ul><li>this is what 99% of the world uses </li></ul></ul></ul><ul><ul><ul><li>$$ </li></ul></ul></ul><ul><ul><ul><ul><li>MSDNAA reduces cost to $800/year for unlimited access </li></ul></ul></ul></ul><ul><li>3) free IDEs </li></ul><ul><ul><ul><li>#develop , a simplified clone of VS.NET </li></ul></ul></ul><ul><ul><ul><li>WebMatrix , for building web-based applications </li></ul></ul></ul>
  7. 7. Hello World in C# <ul><li>Here's the source code: </li></ul>/* hello.cs */ public class Startup { public static void Main() { System.Console.WriteLine(&quot;Hello World!&quot;); } } //class hello.cs
  8. 8. Why System.Console prefix? <ul><li>In .NET, all code & data must live within a module / class </li></ul><ul><li>Often nested within namespaces to help organize things </li></ul><ul><ul><li>a namespace is really just a named collection </li></ul></ul><ul><li>Example: </li></ul>System.Console.WriteLine(&quot;Hello World!&quot;); Console class WriteLine subroutine System namespace in FCL
  9. 9. Compiling and running <ul><li>To compile C# with Framework SDK, use the C# compiler </li></ul><ul><ul><li>open Visual Studio .NET command prompt window to set path </li></ul></ul><ul><ul><li>csc is the command-line C# compiler </li></ul></ul><ul><ul><li>use /t:exe option to specify console-based EXE as target </li></ul></ul><ul><li>To run, simply use name of assembly… </li></ul>c:> csc /t:exe hello.cs Microsoft (R) Visual C# .NET Compiler version 7.00.9466 for Microsoft (R) .NET Framework version 1.0.3705 Copyright (C) Microsoft Corporation 2001. All rights reserved. c:> hello.exe Hello World!
  10. 10. Viewing an assembly with ILDasm <ul><li>IL = Microsoft's Intermediate Language (i.e. generic asm) </li></ul><ul><li>ILDasm = IL Disassembler </li></ul>c:> ildasm hello.exe
  11. 11. IL? <ul><li>Very similar to Java bytecodes: </li></ul><ul><ul><li>generic assembly language </li></ul></ul><ul><ul><li>stack-based </li></ul></ul><ul><ul><li>strictly typed </li></ul></ul><ul><ul><li>no direct memory addressing </li></ul></ul><ul><ul><li>verifiable for safe execution </li></ul></ul>
  12. 12. Part 2 <ul><li>Development on FreeBSD… </li></ul>
  13. 13. Development on FreeBSD <ul><li>Working on FreeBSD is exactly the same! </li></ul><ul><ul><li>i.e. same command-line tools as Framework SDK </li></ul></ul><ul><ul><li>produces *binary-compatible* .DLL and .EXE files! </li></ul></ul>
  14. 14. Part 3 <ul><li>Brief summary of C#... </li></ul>
  15. 15. C# <ul><li>Case-sensitive </li></ul><ul><li>Strict type-checking </li></ul><ul><li>Operator and method overloading </li></ul><ul><li>Single public inheritance; any number of interfaces </li></ul><ul><li>All classes inherit from object; classes may be nested </li></ul><ul><li>Garbage-collected </li></ul><ul><li>Multiple classes may exist in one file </li></ul><ul><li>Multiple files may be compiled into one assembly </li></ul><ul><li>Each assembly typically written in one language </li></ul><ul><li>When in doubt, think Java! </li></ul>
  16. 16. Part 4 <ul><li>Class-based development… </li></ul>
  17. 17. A customer class <ul><li>Here's the source code for a simple Customer class: </li></ul>/* customer.cs */ public class Customer { public string Name; // fields public int ID; public Customer(string name, int id) // constructor { this.Name = name; this.ID = id; } public override string ToString() // method { return &quot;Customer: &quot; + this.Name; } } //class
  18. 18. Main class <ul><li>Here's the source code for Main, using our Customer class: </li></ul>/* main.cs */ public class App { public static void Main() { Customer c; c = new Customer(&quot;joe hummel&quot;, 94652); System.Console.WriteLine( c.ToString() ); } } //class
  19. 19. Compiling and running application <ul><li>Compile and run as before… </li></ul><ul><ul><li>/out: option specifies name of resulting EXE </li></ul></ul><ul><ul><li>in this case we are building monolithic app (single EXE, no DLLs) </li></ul></ul>c:> csc /t:exe /out:app.exe main.cs customer.cs Microsoft (R) Visual C# .NET Compiler version 7.00.9466 for Microsoft (R) .NET Framework version 1.0.3705 Copyright (C) Microsoft Corporation 2001. All rights reserved. c:> app.exe Customer: joe hummel
  20. 20. Part 5 <ul><li>Component-based development… </li></ul>
  21. 21. Example <ul><li>Let's rebuild previous app based on components </li></ul><ul><ul><li>Customer class ==> DLL </li></ul></ul><ul><ul><li>Main class ==> EXE </li></ul></ul>app.exe customer.dll + main.cs customer.cs
  22. 22. Compiling a component <ul><li>Use the C# compiler… </li></ul><ul><ul><li>with /t:library option to specify component library as target </li></ul></ul><ul><ul><li>csc produces a DLL in this case </li></ul></ul>c:> csc /t:library customer.cs Microsoft (R) Visual C# .NET Compiler version 7.00.9466 for Microsoft (R) .NET Framework version 1.0.3705 Copyright (C) Microsoft Corporation 2001. All rights reserved. c:> dir *.dll customer.dll
  23. 23. Compiling and running application <ul><li>Compile using C# compiler as before, except… </li></ul><ul><ul><li>reference component so compiler can locate Customer class! </li></ul></ul><ul><ul><li>reference also stored inside assembly so CLR can locate </li></ul></ul><ul><li>To run, use name of assembly containing Main… </li></ul><ul><ul><li>CLR follows reference to locate DLL </li></ul></ul>c:> csc /t:exe /out:app.exe main.cs /r:customer.dll Microsoft (R) Visual C# .NET Compiler version 7.00.9466 for Microsoft (R) .NET Framework version 1.0.3705 Copyright (C) Microsoft Corporation 2001. All rights reserved. c:> app.exe Customer: joe hummel
  24. 24. Where are references stored? <ul><li>Within assembly as part of assembly manifest </li></ul><ul><li>Visible via ILDasm </li></ul>c:> ildasm app.exe
  25. 25. mscorlib? <ul><li>mscorlib = &quot;ms-core-lib&quot; </li></ul><ul><li>Core FCL assembly </li></ul><ul><ul><li>contains core system classes like string </li></ul></ul><ul><ul><li>contains System.Console class for console-based I/O </li></ul></ul><ul><li>Automatically referenced for us by C# compiler… </li></ul>
  26. 26. Recall CLR-based execution <ul><li>All assemblies must be present: </li></ul>.EXE other FCL assemblies CLR obj code OS Process Underlying OS and HW Core FCL assembly .DLL .DLL .DLL obj code obj code obj code JIT Compiler
  27. 27. Summary <ul><li>.NET is multi-language </li></ul><ul><ul><li>Framework SDK based on C# and VB.NET </li></ul></ul><ul><ul><li>lots of other languages available </li></ul></ul><ul><li>.NET development is component-based </li></ul><ul><ul><li>helper classes implemented in one or more DLLs </li></ul></ul><ul><ul><li>EXE implemented using helper classes </li></ul></ul><ul><ul><li>if (assembly A uses a class from assembly B) then A must reference B! </li></ul></ul>
  28. 28. References <ul><li>Books: </li></ul><ul><ul><li>J. Richter, &quot;Applied Microsoft .NET Framework Programming&quot; </li></ul></ul><ul><li>Web sites: </li></ul><ul><ul><li>http://msdn.microsoft.com/net </li></ul></ul><ul><ul><li>MSDNAA: http:// www.msdnaa.net/ </li></ul></ul><ul><ul><li>Rotor (SSCLI): http://msdn.microsoft.com/net/sscli </li></ul></ul><ul><ul><li>Mono: http:// www.go-mono.com/ </li></ul></ul><ul><ul><li>Free IDEs: </li></ul></ul><ul><ul><ul><li>http://www.icsharpcode.net/OpenSource/SD/default.asp </li></ul></ul></ul><ul><ul><ul><li>http://www.asp.net/webmatrix/ </li></ul></ul></ul><ul><ul><li>Anakrino reverse-engineering tool: </li></ul></ul><ul><ul><ul><li>http:// www.saurik.com/net/exemplar/ </li></ul></ul></ul>
  29. 29. Lab? <ul><li>Work on lab #1, &quot;Architecture&quot;… </li></ul>

×