slide1 l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Programming with C# and .NET PowerPoint Presentation
Download Presentation
Programming with C# and .NET

Loading in 2 Seconds...

play fullscreen
1 / 181

Programming with C# and .NET - PowerPoint PPT Presentation


  • 712 Views
  • Uploaded on

Programming with C# and .NET Outline 1 Demo .NET introduction C# Programming 2 3 .NET Remoting 4 Programming with C# and .NET Demo The Basic Idea C# is the programming language that I am long waiting for, because my bad MFC experience. Question: How to use C# to do

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Programming with C# and .NET' - medwin


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

Programming with

C# and .NET

outline
Outline

1

Demo

.NET introduction

C# Programming

2

3

.NET Remoting

4

slide3

Programming with

C# and .NET

Demo

slide4

The Basic Idea

  • C# is the programming language that I
  • am long waiting for, because my bad
  • MFC experience.
  • Question: How to use C# to do
    • Image Processing
    • OpenGL Programming(3D Graphics)
    • DirectX Programming
  • SEE DEMO
slide5

Programming with

C# and .NET

.NET

slide6

Major Components

  • CLR
    • Common Language Runtime
    • a runtime environment
    • concept similar to JVM
  • FCL
    • Framework Class Library
    • built on top of the CLR
    • provide services for modern applications
slide7

FCL

CLR

Windows API

Windows Operating System (Windows ME, 98, 2000, XP etc)

.NET Framework Overview

Applications written in J# .NET, VB .NET, or C#

slide8
MSIL
  • Microsoft Intermediate Language
  • a CPU independent set of instructions
  • .NET compliant language compile into

MSIL

  • similar to Java Byte Code
  • sometimes abbreviated as IL
slide9

.NET

Visual J# .NET

C#

VB .NET

Compile into MSIL

MSIL

CLR do this

Linux native code

Windows native code

Mac OS native code

Will Support soon

Support now

Will Support soon

slide10

Java

Java

Java Byte Code

JVM do this

Linux native code

Windows native code

Mac OS native code

net compliant languages
.NET Compliant Languages
  • Any language that can be compiled into

MSIL is called a .NET compliant language

  • APL, Pascal, Perl, Python, Scheme, Eiffel,

Fortran, Java, Jscript, Haskell, COBAL,

RPG, APL, Smalltalk, Component Pascal,

Curriculum, Mercury, Oberon, Oz, VB .NET

, C#, Visual C++ .NET, Visual J# .NET, …

msil advantages
MSIL Advantages
  • Portability between OS
    • .NET compliant language are all compiled into

MSIL (portable between OS) and can be further

compiled into native OS machine codes by CLR

  • Language Interoperability
    • Different languages can communicated easily
    • MSIL codes from different languages can be

linked together to form a program

slide13

MSIL

MSIL

MSIL

Interoperability

Visual J# .NET

C#

VB .NET

Compile into MSIL

linked the MSIL codes

CLR generated a single application (native code)

Windows native code

slide14

Language Interoperability

  • Rules defined in
    • Common Type System (CTS)
    • Common Language Specification (CLS)
  • Cross-language development
    • Cross-language Debugging
    • Cross-language Exception Handling
    • Cross-language Inheritance
slide15

Common Type System (CTS)

  • To unify the data types
    • Common data types play an important role in
    • language interoperability
  • Types can be of two types
    • Value Type
    • Reference Type
slide16

.NET vs. Java

  • Runtime environment
    • .NET  CLR
    • Java  JVM
  • Intermediate Code
    • .NET  MSIL
    • Java  Java Byte Code
  • Support
    • .NET  Multiple Languages, Multiple Platform
    • Java  Single Language, Multiple Platform
slide17

CLR

  • Load and execute the C # program
  • Compile the MSIL into native code
    • use Just-in-Time (JIT) compilers
  • Garbage Collection
    • use Garbage Collector (GC)
  • Security Management
  • Exception Handling
slide18

Managed vs. Unmanaged Code

  • Managed Code
    • executed under the control of CLR, and use
    • the .NET Framework libraries.
  • Unmanaged Code
    • does not execute under the CLR
  • It is possible for managed and unmanaged
  • code to work together
slide19

FCL

  • concept similar to MFC for Windows
  • programming
  • FCL classes are grouped by namespaces and
  • exported by assemblies
    • namespace similar to Java package
    • assembly similar to .dll
slide20

FCL

  • Some Namespaces in FCL (has hierarchy)
    • System
    • System.IO
    • System.Windows.Forms
    • System.Drawing
  • Example:
    • System.Windows.Forms is located in
    • System.Windows.Forms.dll
slide21

CLR vs. CLI

  • CLI (Common Language Infrastructure)
  • CLR vs. CLI
    • CLI is the rule
    • CLR is the implementation
  • your own CLR
    • You can implement your own CLR according
    • the CLI
slide22

MSIL vs. CIL

  • CIL (Common Intermediate Language)
  • MSIL vs. CIL
    • CIL is the rule
    • MSIL is the implementation
  • your own IL
    • You can implement your own IL according
    • the CIL
slide23

Web Services

  • ASP .NET
    • host on IIS server
  • .NET Remoting
    • can host on any type of applications
slide24

Programming with

C# and .NET

Windows Programming so easy!

C#

slide25

Anders Hejlsberg

  • Creator of
    • C#
    • Turbo Pascal
    • Delphi
  • Anders studied engineering at the Technical
  • University of Denmark, previously worked for
  • Borland, now works for Microsoft.
slide26

Special features

  • Properties
  • Indexers
  • Delegates and Events
  • Operator Overloading
  • Reflection
  • Attributes
  • Formatting
  • Regular Expression
slide27

Pointer

  • Miscellaneous features
    • jagged array
    • foreach loop
    • Runtime type identification (RTTI)
    • goto
    • structure (not the same with C/C++)
slide29

1. Simple Console Program

link

  • Visual Studio .NET IDE introduction
  • C# program overview
  • System.Console.WriteLine(…);
  • Build and Run C# program

namespace

method

class

slide36

link

2. Rapid Application Development

  • RAD like Visual Basic and Borland C++
  • Builder
  • concise syntax as Java
  • event-driven programming style
slide46

3. Use Assembly

link

  • classes may be compiled into .exe or .dll,
  • such files are called assemblies and are the
  • packaging units of C#
  • An assembly is composed of four sections
    • manifest
    • type matadata
    • program code (in MSIL format)
    • resources used by the program
slide47

Manifest

    • contain information about the assembly itself
  • Type matadata
    • information about the data types used by the
    • program
  • Program Code
    • stored in MSIL format
  • Resources used by the program
    • such as .bmp or .jpg files
slide52

4. Namespaces

  • Namespace prevent name conflicts
  • A namespace can be
    • split over several files
    • separated within the same files
  • Namespaces can be nested
slide53

Example 1

A namespace split over several files

link

slide55

Example 2

Namespaces Prevent Name Conflicts

link

The same class name

The same method name

slide56

Example 3

Namespaces Can Be Nested

link

slide58

5. Properties

  • Question:
  • How do you access private data in Java?
  • Properties provide the illusion that we
  • can use private data directly
slide59

example:

Property

slide61

6. Indexers

  • An indexer allows an object to be indexed like an array
  • example:
slide63

set

get

slide64

7. Delegate & Event

  • A delegate is an object that can refer to a method
  • A delegate can invoke
    • instance method associated with an object
    • static method associated with a class
  • A delegate is similar to a function pointer in C/C++
  • A delegate supports Multicasting
slide65

Anobject that has an interest in an event registers an handler for that event

  • When the event occurs, all registered handlers are called.
  • Event handlers are represented by delegate.
  • You can use event accessors to change the way the event handlers are add to or remove from the invocation list
slide66

Example 1

Call instance method

link

slide68

Example 2

Call class static method

link

slide70

Example 3

Delegate supports multicasting

link

slide73

Example 4

Simple Event & Delegate Demo

link

Class static method

Delegate

slide74

8. RTTI

  • Runtime type identification(RTTI) allows the type of an object to be determined during program execution.
  • There are three keywords support RTTI
    • is
    • as
    • typeof
slide75

Example 1

RTTI Demo

link

slide77

9. Reflection

  • System.Type is at the core of the reflection sub-system
  • Remember
    • using System.Reflection;
  • Several commonly used methods defined by Type
    • ConstructorInfo[ ] GetConstructors( )
    • EventInfo[ ] GetEvents
    • FieldInfo[ ] GetFields
slide78

MemberInfo[ ] Getmembers()

  • MethodInfo[ ] GetMethods()
  • PropertyInfo[ ] GetProperties()
slide79

Example 1

Obtain class method

link

slide80

Example 2

Obtain class constructor

link

slide81

Example 3

Obtain Types from Assemblies

link

MyClass.cs

Compile the MyClass.cs into a MyClass.dll, you need to

1. Locate the csc.exe, and set the path

2. csc /t:library MyClass.cs

slide84

Example 4

How to create and use a DLL with MS Visual Studio .NET

link

Step 1

slide85

2

1

3

No Main()

slide87

10. Pointer

  • Enable C# to use C/C++ high-performance,
  • systems code
  • Code must be marked as unsafe
    • unsafe code does not execute under the full
    • management of the CLR
  • When a pointer points to a managed variable,
  • it must use fixed to prevented the variable
  • from being moved by the garbage collector.
slide88

Example 1

Simple pointer demo

link

slide90

Example 2

Using fixed

link

t should be fixed in one location while p was point to &t.num

Managed object

slide91

11. The object class

  • base class of all C# classes
  • object is just another name for System.Object
  • used inBoxing & Unboxing
    • Boxing : an object reference refers to a value type
    • Unboxing : retrieve a value from a object
  • as a Generic Data type
slide92

Example 1

Boxing and Unboxing

link

slide93

Example 2

object as a generic data type

link

slide94

12. ref & out

  • parameter passing
    • Value Type : pass by value
    • Reference Type : pass by reference
  • ref&out let you pass value type by reference
slide95

Example 1

ref & out

link

slide96

Example 2

swap with ref

link

slide97

13. Inheritance

  • a derived class inherits all of the variables, methods, properties, and indexers defined by the base class and add its own unique elements.
  • Three major topics when using inheritance
    • Data
    • Constructor
    • Methods
  • Polymorphism
slide98

Example 1

Access bass class’s private data through properties

link

slide99

Example 2

Calling Base Class Constructor

link

slide101

Example 3

Inheritance and Name Hiding

link

slide103

Example 4

Using base to access a hidden item

link

slide104

Example 5

Virtual Methods and Overriding (polymorphism and dynamic binding)

link

slide107

Example 6

Using sealed to prevent inheritance

link

slide109

14. Interface

  • No data members
  • No constructors, destructors
  • Not allow static member
  • Class members have no implementation
  • Many classes can implement the same
  • interface
  • When a class implements an interface, the
  • class must implement the entire interface.
slide110

Class can implement more than one inter-

  • face. The interfaces are separated with a
  • comma.
  • A class can inherit a base class and also
  • implement one or more interface. In this case,
  • the name of the base class must come first.
  • The method that implement an interface must
  • be declared public.
  • Interface can be inherited
slide111

Example 1

Interface Properties

link

slide112

Example 2

Interface Indexers

link

slide114

Example 3

Interface can be inherited

link

slide115

Example 4

Interface Polymorphism

link

slide116

15. Structures

  • A structure is similar to a class, but it is of
  • value type.
  • cannot inherit or be a base for other struc-
  • tures or class (but it inherit object)
  • can implement one or more interface
  • can define constructors, but not destructors
slide117

However, you cannot define a default const-

  • ructor (no parameters)
  • can be created using new or performed the in-
  • itialization manually.
  • a struct was accessed directly, not through
  • reference variable, so it saved space and got
  • more efficiency.
slide118

Example 1

Structure Demo

link

slide120

Programming with

C# and .NET

.NET Remoting

outline121
Outline

1

Introduction

Basic Architecture

Examples

2

3

Conclusion

4

slide123

1. Basic Model

Client

Server

Proxy

1

Remote Object

9

2

8

3

Formatter

Formatter

7

4

6

Client Channel

Server Channel

5

slide124

2. Calling Procedure

Client Side

client called the proxy. For the client, the proxy looks like the real object with the same public methods.

1

When the methods of the proxy are called, messages will be created.

2

The messages are serialized using a formatter class, and are sent into a client channel.

3

4

slide125

The client channel communicates with the server channel to transfer the message across the network.

5

Server Side

The server channel sends the serialized data to a formatter.

6

The formatter deserialized the message.

7

The deserialized messages are then dispatched to the remote object.

8

slide126

3. Configuration Option

SingleCall

Well-known

Singleton

Remote Objct

Client-activated

slide127

SOAP

Formatter

Binary

TCP

Channel

HTTP

slide128

4. Related Technology

DCOM

1

Java RMI

2

CORBA

3

slide130

Architecture Overview

  • Remote Objects
    • Well-known: SingleCall vs. Singleton
    • Client-activated
  • Activation
    • Server-activated object (SAO)
    • Client-activated object (CAO)
  • Channel
    • TCP
    • HTTP
slide131

Formatter

    • Binary
    • SOAP
  • Proxy
    • Transparent
    • Real
  • Marshaling
    • Marshal-by-value (MBV)
    • Marshal-by-reference (MBR)
slide132

Lease-Base Lifetime

    • lease.RenewOnCallTime
    • sponsor.RenewalTime
  • Lease machanism is for long-lived objects
  • Well-known singleton
  • Client-activated

SingleCalltypes do not participate in the lifetime lease system.

slide133

1. SingleCall

Client

Server

Remote object

Remote object

Remote object

slide134

Did not cause the server to create a remote object

each method call caused the server to create a new remote object

slide136

Every remote method callwillcreate a

  • new remote object on the server
  • SingleCall types do not participate in the
  • lifetime lease system
  • remote objects will automatically be
  • garbage collected after the call complete
  • useful when objects are required to do a
  • finite amount of work
slide137

Example 1

programmatic configuration

link

slide138

Assembly

Endpoint

slide140

Example 2

with configuration file

link

slide145

2. Singleton

Client

Server

Remote object

slide146

Did not cause the server to create a remote object

A remote object was created for the first method call.

All of the clients will share the same remote object.

slide148

The firstremotemethod callwill create a

  • remote object on the server
  • Multiple clients be serviced by onlyone
  • remote object
  • be careful aboutthe concurrency and data
  • protection problem
  • use lease-based lifetime mechanism
slide149

3. Client-activated

Client

Server

Remote object

slide150

Cause the server to create a remote object

Did not create any more remote object

slide152

Example 1

programmatic configuration

link

slide155

Example 2

with configuration file

link

slide160

4. Lease-Based Lifetime

  • Long-lived remote objects use lease
  • machanism for their objects lifetime
  • Two ways to extend the lifetime
    • Clients make remote method calls
    • use a sponsor
  • When the leasing time is expired, the sponsor
  • is asked if it extends the lease.
slide161

A value is defined with RenewOnCallTime

  • to extend the leasing time when client calls
  • the method of the remote object
  • The ISponsor interface defines the method
  • Renewal( )
  • The class ClientSponsor provides a default
  • implementation for ISponsor interface.
slide162

Client

Server

Remote object

RenewOnCallTime

CurrentLeaseTime

If (CurrentLeaseTime >= RenewOnCallTime) {//Do nothing}

else {CurrentLeaseTime= RenewOnCallTime;}

slide163

Client-activated

Client

Server

Remote object

Leasing time expired

Cause a exception

slide165

Well-known singleton

Remote object

Server

Client

Leasing time expired

Remote object

Create a new remote object

slide167

Example 1

CAO lease.RenewOnCallTime

link

slide168

Example 2

sponsor

link

slide170

5. Marshal-By-Value

Client

Server

a

a

slide171

Marshaling means converting the object in

  • order to send it across the network (or
  • across processes or application domains)
  • With MBV the object is serialized into the
  • channel, and a copy of the object is created
  • on the other side of the network
  • The class must be marked with the attribute
  • [Serializable]
slide172

Example 1

return object with MBV

link

slide175

6. Marshal-By-Reference

Client

Server

a

proxy

slide176

MBR creates a proxy on the client that is

  • used to communicate with the remote
  • object
  • The class must derived from
  • MarshalByRefObject
slide177

Example 1

return object with MBR

link

slide181

Conclusion

.NET Remoting is built on a layered model, with each layer replaceable by custom code created by a developer. Therefore, new messaging, transport, and communication protocols can be implemented and plugged in as needed. Thus we can apply it to our distributed or web service system with least difficulties and at the same time have higher performance or interoperability than other technology can provide.