Sie sind auf Seite 1von 14

Introduction 1

C# 30

C# 3.0
Chapter 1 Introduction

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 2

C# 30

Course Objectives
Learn to program with
ith the C# lang
language
age &
the .NET environment
Discover its concepts and features
Find out where C# follows C++ and where it presents
new concepts and techniques

Deal
D l with
ith severall .NET
NET generall ttopics
i
The .NET p
platform p
provides lots of tools for
developing .NET target applications, as well as
playing a major role in the applications execution

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 3

C# 30

Course Objectives
The .NET
NET en
environment
ironment is a huge
h ge world
orld
We will discuss only those .NET issues that directly
relate to C# programming

Knowing C# is an excellent basis for


further advancing in different .NET
NET paths
The rest of this chapter will introduce
some basic .NET
NET concepts

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 4

C# 30

Microsoft .NET
NET Initiative
Behind the .NET
NET Framework motivation
stands a vision
To be the best platform for building integrated,
service- oriented applications
Applications that gather information from, and interact
with, a wide variety of sources, regardless of
platforms or languages in use

Although there is a NET in .NET


The framework can be used in almost any kind of
application (even micro-devices)

.NET is a revolution based on a


technologies evolution
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 5

C# 30

Microsoft .NET
NET Initiative
In the past,
past creating integrated
pp
was not a simple
p task
applications
It involves a combination of different technologies
For example:
D/COM, WIN32, VBA, Java Applets, ASP/Interdev, ADO,

We needed to use COM with Type Libraries or


Interface Definition Language to glue them together

COM iis a wonderful


d f l technology
h l
that
h
support components reuse
But there are many limitations such as not having an
Object Oriented Inheritance mechanism
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 6

C# 30

Microsoft .NET
NET Initiative
The .NET
NET platform simplifies integrated
application development, by:
Replacing old models with new ones
COM Lifetime, Memory, threading, & Error Management

Simplifying the use of modern technologies


SOA, RIA, Rich Client Applications, DB Access, Transactions

In different
ff
programming languages:
C#, VB.NET, F#, C++/CLI, etc.

Integrating
I t
ti allll kinds
ki d off technologies:
t h l i
A Windows GUI application as a front end to a Web Service

All off th
these are easily
il achieved,
hi
d using
i th
the
unified .NET environment
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 7

C# 30

The .NET
NET Framework
The ttwo
o main .NET
NET Frame
Framework
ork
p
are:
components
The Common Language Runtime (CLR)
The execution engine for .NET Framework applications
A rich runtime, providing applications with lots of important
services

The Framework Class Library


An hierarchical set of classes that p
provides access to the
frameworks functionality and services
It also provides higher-level development constructs
collection classes, GUI classes, etc.

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 8

C# 30

The Common Language Runtime


The CLR is the execution engine
It is responsible for application code loading and
execution

One of the main CLR tasks is memory


management:
The .NET environment is a managed
managed environment
environment
The CLR is responsible for memory allocation and
deallocations
It uses a garbage collection mechanism to reclaim the
y of unreferenced objects
j
memory

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 9

C# 30

The Common Language Runtime


Among other CLR ser
services
ices we
e can find
find:
Conversion of intermediate language to native code
Code access security enforcement:
The .NET has a well-defined securityy model,, which better
meets todays needs, where applications are installed and
run through the Internet

Cross-language exception handling mechanism


Exception handling is the error reporting method used in the
.NET
NET Framework
F
k
It has underlying CLR support, common for all .NET
languages

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 10

C# 30

The Common Language Runtime


Among other CLR ser
services
ices we
e can find
find:
Interoperability services with unmanaged code:
Code that was not developed under the .NET Framework,
such as: COM objects, and C/C++ based DLLs

Runtime access to modules and types metadata


Support for developer services
Profiling, debugging, and so on

Most of the topics mentioned above will be


di
discussed
d llater
t iin th
the course
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 11

C# 30

.NET
NET Framework Fundamentals
The .NET
NET framework is a multi
multi-language
language
environment
While the COM model enables interoperation
between different language components, the .NET
enables
bl reall integration
i t
ti between
b t
them:
th
We can reuse code from various languages
We
W can declare
d l
a class
l
iin VB
VB.NET
NET and
dd
derive
i ffrom it iin C#

How is this multi-language integration


achieved?

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 12

C# 30

The Common Type System


The .NET
NET Frame
Framework
ork defines a Common
yp System
y
((CTS):
)
Type
A type system which is common to all .NET
languages
The CTS defines the different available types
Classes,
Classes structures
structures, interfaces
interfaces, enum types,
types etc
etc.

The CTS defines the possible inner structure of a type


Methods,
Methods fields
fields, properties
properties, events
events, operators

The CTS also defines a set of primitive types, and


various type conversion rules

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 13

C# 30

The Common Type System


.NET
NET lang
languages
ages map their ttypes
pes to the
yp
CTS defined types
That means that all .NET languages share the same
type system
This was one of the main obstacles for language
commonality and integration

All the languages also share the same


compiled
il d bi
binary fform, th
the MSIL
This allows seamless interaction between different
.NET languages

C# supports
the main features of .NET
pp
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 14

C# 30

Common Language Specification


CTS

CLS
Th Common
C
T
S t
i ttoo bi
i l llanguage
The
Type
System
is
big ffor any single
The Common Language Specification is for cross
cross--language use

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 15

C# 30

A .NET
NET Application General Workflow
Source Code
(in any .NET
aware language)

Compiler

EXE / DLL (IL +


Metadata)

Just-in-time
Compiler

Class loader

Runtime

Platform Specific
Instructions

Target Program
Execution

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 16

C# 30

Microsoft Intermediate Language


MSIL is a high
high-level
level machine language
It has instructions that

Create
C
t and
d iinitialize
iti li objects
bj t
Call virtual methods on objects
Manipulate array elements directly
Raise and catch exceptions

MSIL is not tied to any specific CPU platform


It is an execution stack-based machine language
A PE file containing MSIL can run on any CPU

There are many .NET environments


MS-Windows (x86/x64/IA-64), .NET CF, Silverlight
(Windows & Mac), Mono (UNIX/LINUX), XNA (XBOX)
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 17

C# 30

MSIL Example
Hello World application in VB
VB.NET
NET

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 18

C# 30

MSIL Example
Hello World application in C#

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 19

C# 30

MSIL Example - ILDASM

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Introduction 20

Converting VB to C# using Reflector

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 21

C# 30

The Just-in-Time Compiler


A .NET
NET language compiler compiles the
source code into MSIL
Located in a PE file

The MSIL is then translated into native


code using a JIT compiler
The JIT is platform specific
specific, and therefore can
produce the most efficient code per a specific
platform
The JIT can also create an efficient code using inline
p
optimization
It can also do that across assemblies methods

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 22

C# 30

The Two Jitters


JIT (the default)
Converts the code to machine language on the fly
Each method is compiled the first time it is invoked
Compiled methods are cached until application exit

Install-time code generation (NGEN.EXE)


A tool (EXE & Windows Service) that converts MSIL
to CPU machine code (typically at installation time)
It may improve
i
lload
d ti
time & runtime
ti
performance
f
Whether or not you use it depends on the type of your
application, and the target machine

Test your code to decide which JIT to use


Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 23

C# 30

The Base Class Library


The .NET
NET BCL is a librar
library of ttypes
pes that
provide access to system
p
y
functionality
y
Offering many higher-level services, commonly
required throughout application development
It is also known as the Framework Class Library
(FCL)

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 24

C# 30

Win32 Replacement
The .NET
NET BCL is designed to simplify
platform service access:
Services are provided through classes
Each class represents a functional entity, and provides all its
related functions

Classes are defined within different namespaces


Based on their functionality subject (e
(e.g.
g System
System.IO,
IO
System.Security)

Using function overloading,


overloading
Different flavors of the same service can be provided in a few
versions of the same function name, differing in their
arguments
t

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 25

C# 30

.NET
NET Framework Namespaces
Namespace

Contained Types Description

System

Core classes and base classes for commonlyused types and operations:
Primitive types and conversions, mathematical
manipulations, garbage collection, exception
handling and more

System.Collections.Generic

Container classes (e.g. List<T>, Queue<T>,


Stack<T> Dictionary<Tkey,
Stack<T>,
Dictionary<Tkey Tvalue>,
Tvalue> etc.)
etc )

System.Data

Database manipulation facilities.

System Drawing
System.Drawing

GDI+ types: bitmaps


bitmaps, fonts
fonts, icons
icons, etc
etc.

System.IO

IO manipulation types: file IO, buffering, etc.

System.Net

Network programming related types (sockets,


request/response, etc.)

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 26

C# 30

.NET
NET Framework Namespaces
Namespace

Contained Types Description

System.Diagnostics

Diagnostics services such as Debugging,


Asserting Logging & Performance
Asserting,
Counters

System.Reflection

Reflection related types

System.Runtime.InteropServices COM servers and Win32 DLLs


interoperability facilities
System.Runtime.Remoting

Remoting related facilities

System.Security

Security related types (permission,


cryptography, policy, etc.)

System.Serialization

Serialization and deserialization services

System.Threading

Threading related types (Mutex, Thread,


ThreadPool, Timer, etc.)

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 27

C# 30

.NET
NET Framework Namespaces
Namespace

Contained Types Description

System.Configuration

Used to programmatically access .NET


Framework configuration
(ConfigurationManager, AppSettingSection, etc.)

y
System.Web

Provides classes and interfaces that enable


browser/server communication (HTTPRequest,
HTTPResponse, etc.).

S t
System.WebServices
W bS i

Cl
Classes
ffor creating
ti and
d using
i W
Web
bS
Services
i

System.Windows.Forms

Classes for Windows GUI application


development (controls, dialog boxes, etc.).

System.Xml

Classes that provide standards-based support


for processing XML
XML.

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Introduction 28

C# 30

Chapter Summary
Course
Co rse Introd
Introduction
ction
In this course you will learn how to program with the
C# language & the .NET environment
The .NET environment is a huge
g world,, this course,,
however, will discuss only those .NET issues that
directly relate to C# programming

.NET Environment & Framework


Introd ction
Introduction
CLR, CTS, CLS, GC, MSIL, JIT, ILDASM, NGEN,
BCL

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Das könnte Ihnen auch gefallen