2. Developing in .NET and C#

Download Report

Transcript 2. Developing in .NET and C#

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