Transcript .NET

Lecture 1: .NET
What Is It And Why Use It?
7/18/2015
.NET - What and Why
1
Software from Components
• In the industrial revolution, engineers learned
to build things in a consistent, predictable,
repeatable way
• Design once, build many identical parts
• They learned to use assembly lines to assemble
multiple items from sets of identical components
• Any component of a given type and
specification is interchangeable with another
of the same type
7/18/2015
.NET - What and Why
2
Why not build software the same way?
• Multiple languages and incompatibilities
• Basic, C, C++, COBOL, Visual Basic, C#, F#, Java,
Fortran, Perl, Python, Eiffel, Delphi, SQL, Pascal,
PL/I, APL, assembly, Scheme, Smalltalk, Prolog,
Lisp, RPG, Ada, Snobol, Forth, Algol, Modula-2,
HTML, Haskell, JavaScript, Objective C, ABAP, …
• Cannot just take a block of code from a Lisp
program, for example, and plug it into a COBOL
program and expect it to work
• Usually, cannot easily call an Ada method from a
Python program, for example
7/18/2015
.NET - What and Why
3
Why not build software the same way?
• Different Data Types
• Many different data types
• Not implemented the same way in all
languages
• Implementations vary in size
• Character type may take 1 byte (ASCII or
EBCDIC) or 2 or more bytes for Unicode
• Integers may occupy 8 bits, 16 bits, 32 bits, 64
bits, 128 bits, . . .
7/18/2015
.NET - What and Why
4
Why not build software the same way?
• Different conventions
• Argument passing may be left-to-right or rightto-left
• Arguments in one language may be passed
using a stack; others use registers, pointers,
or some other means; not consistent among
languages
• Name collisions
• Same term may be used differently in different
languages – a class name in one language may
be a keyword in another
7/18/2015
.NET - What and Why
5
Why not build software the same way?
• Different platforms and architectures
• Addresses and data: 16-bit, 32-bit, 64-bit,…
• Big-endian (ABCD) vs. little-endian (DCBA)
• Different instruction sets
• Differences in register architecture
7/18/2015
.NET - What and Why
6
Why not build software the same way?
• Different operating systems even on the
same hardware
• Almost all user software depends on and
uses services provided by the OS and its
subsystems
• Different Operating Systems have different
ways of implementing the services, different
API’s, different conventions, different security
approaches, different levels of support for
various activities
7/18/2015
.NET - What and Why
7
Why not build software the same way?
• Different Human Languages; Different Cultures
• English, French, Japanese, Chinese, German, Spanish,
Arabic, Russian, Hebrew, …
• In the US, 1.23 represents 1 and 23 hundredths, while in
many European cultures, the same value would be
represented as 1,23
• Fahrenheit vs. Celsius; in US, 30 degrees is cold while
in Canada, 30 degrees is hot
• US Dollars vs. Canadian Dollars, Euros, Yen, Rubles,
Riyals, Dirhams, Pesos, Yuan, ...
• Dates: 03/04/2014 is March 4, 2014 in the US, but it is
April 3, 2014 in much of Europe
• Inches, miles, gallons, pounds vs. centimeters,
kilometers, liters, and kilograms . . .
7/18/2015
.NET - What and Why
8
COM, CORBA, Enterprise Java Beans
• Various companies and groups of
companies came up with approaches
that could be used to standardize things
and allow components to work together
• Problems:
• All approaches were proprietary
• Approaches incompatible with each other
• Not based on open standards
7/18/2015
.NET - What and Why
9
.NET
• Based on a standard developed by Microsoft,
Intel, IBM, and others
• Approved as a standard by ECMA
• Approved as a standard by ISO
• Standards cover CTS, CLS, C#, and other items
• Designed to be language agnostic
• Platform neutral
• Open to development by anyone
7/18/2015
.NET - What and Why
10
Many others including
IronPython, IronRuby,
Cobol, Delphi,
JavaScript, etc.
The virtual
machine
runs this
7/18/2015
.NET - What and Why
11
Common Type System (CTS)
• All .NET languages support common types –
though not necessarily using the same names
• System.Int32 is called int in C++ and in C#, and
Integer in VB, but they are same type and can be
passed back and forth as arguments
• System.Single is called Single in VB and float in
C++ and C#, but all are the same type and are
interchangeable in the .NET languages
7/18/2015
.NET - What and Why
12
Common Language Runtime (CLR)
• All .NET compilers emit platform-neutral Intermediate
Language (IL) object code (like byte-code in Java)
rather than native machine language code
• IL is the same regardless of hardware, OS, or .NET
language
• Output of a project is called an Assembly: may be either a
.EXE or a .DLL
• Only the CLR needs to know on what platform it is
running
7/18/2015
.NET - What and Why
13
CLR, continued
• CLR contains a Just-In-Time (JIT) compiler that turns
IL into native machine language optimized for its
target machine
• Very fast and efficient
• Done only once and only if needed
• Thus code is compiled, not interpreted as in some
languages
• CLR also handles garbage-collection, exception
handling, cross-language debugging, and
distributed debugging, and other common features
• Includes runtime support for thousands of .NET
classes
7/18/2015
.NET - What and Why
14
IL Example
.method public hidebysig static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = Code size
14 (0xe)
.maxstack 8
IL_0000: nop
IL_0001: newobj instance void ManageDB.frmTblMgmt::.ctor()
IL_0006: call
void
[System.Windows.Forms]System.Windows.Forms.Application::Run(class
[System.Windows.Forms]System.Windows.Forms.Form)
IL_000b: nop
IL_000c: nop
IL_000d: ret
} // end of method frmTblMgmt::Main
7/18/2015
.NET - What and Why
15
Runtime Compilation and Execution
Form1
Which language?
C# code
C# Compiler
JIT compiler
Visual Basic .NET code
VB.NET compiler
MSIL
CLR
Native
code
7/18/2015
.NET - What and Why
16
Platform Neutral
• Because the IL code is not targeted at any
platform, it is portable between systems of
different HW, SW, etc.
• A (.NET) .exe and a (.NET) .dll compiled on
a Windows system will run on a Mac, a
Sun, or an IBM mainframe
. . . IF . . .
the target machine has its own CLR with a JIT
compiler to convert the IL code to native code
targeted to the machine on which it is to run and to
provide the runtime support for the .NET classes
7/18/2015
.NET - What and Why
17
Language Interoperability
• The standards that are part of .NET insure that:
• A Windows control developed in VB.NET can
be used in a C# program
• A method written in a business-tier class in
COBOL.NET can be invoked by a VB.NET
Windows Forms front end
• A .NET string in a Delphi.NET program
compiled using a Borland compiler on a
Windows computer can be passed to a C#
method written using a SSCLI compiler running
on an Apple OS-X platform
7/18/2015
.NET - What and Why
19
.NET Implementations
• Implementations of .NET include
• Microsoft’s .NET Framework and Visual Studio.NET
• The Shared Source Common Language Initiative (SSCLI) that
runs on BSD Unix and Apple OS-X
• Mono - open source effort up until recently led by Novell and
others
• More than 30 languages support .NET:
• Microsoft: VB.NET, C#, managed C++, Cω, Spec#, F#
• Python, Perl, Cobol, Delphi, Pascal, Eiffel, Fortran, RPG,
Scheme, Smalltalk, Ruby, Forth, and many others by nonMicrosoft vendors
• Thousands of tools are available from third-party vendors to aid in
.NET Framework development, including more than 1000 add-ins
for Visual Studio .NET, as well as compilers with their own IDE’s
from Borland, Mono project, and Macromedia
7/18/2015
.NET - What and Why
20
Thus, …
• Solutions/Applications can be developed in
any .NET language or languages (by project)
that fully supports the features used by the
application and that adheres to the standards
• Different parts of the application (solution) can
be developed in different languages
• A VB main program can use a class developed in C#
that uses another class developed in F#, for example
• In a .NET web application, each web page at a web
site could be developed in a separate .NET language
7/18/2015
.NET - What and Why
22