C sharp

181
Programming with Programming with C# and .NET C# and .NET

description

 

Transcript of C sharp

Page 1: C sharp

Programming withProgramming with

C# and .NETC# and .NET

Page 2: C sharp

Outline

Demo

.NET introduction

C# Programming

2

3

1

4 .NET Remoting

Page 3: C sharp

Programming withProgramming with

C# and .NETC# and .NET

DemoDemo

Page 4: C sharp

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

Page 5: C sharp

Programming withProgramming with

C# and .NETC# and .NET

.NET.NET

Page 6: C sharp

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

Major Components

Page 7: C sharp

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

CLR

FCL

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

Windows API

.NET Framework Overview

Page 8: C sharp

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

Page 9: C sharp

C# VB .NET Visual J# .NET

MSIL

Linux native code

.NET

Windows native code

Mac OS native code

Compile into MSIL

CLR do this

Support now

Will Support soon

Will Support soon

Page 10: C sharp

Java

Java Byte Code

Linux native code

Java

Windows native code

Mac OS native code

JVM do this

Page 11: C sharp

.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, …

Page 12: C sharp

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

Page 13: C sharp

C# VB .NET Visual J# .NET

MSIL

Interoperability

Windows native code

Compile into MSIL

linked the MSIL codesMSIL MSIL

CLR generated a single application (native code)

Page 14: C sharp

Rules defined in Common Type System (CTS)

Common Language Specification (CLS)

Cross-language development Cross-language Debugging

Cross-language Exception Handling

Cross-language Inheritance

Language Interoperability

Page 15: C sharp

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

Page 16: C sharp

.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

Page 17: C sharp

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

Page 18: C sharp

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

Page 19: C sharp

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

Page 20: C sharp

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

FCL

Page 21: C sharp

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

Page 22: C sharp

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

Page 23: C sharp

Web Services

ASP .NET host on IIS server

.NET Remoting can host on any type of applications

Page 24: C sharp

Programming withProgramming with

C# and .NETC# and .NET

C#C#

Windows Programming so easy!

Page 25: C sharp

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.

Page 26: C sharp

Special features

PropertiesIndexers

Delegates and EventsOperator OverloadingReflectionAttributesFormattingRegular Expression

Page 27: C sharp

Pointer Miscellaneous features

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

Page 28: C sharp

Start Programming

Page 29: C sharp

1. Simple Console Program

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

Build and Run C# program

namespaceclass

method

link

Page 30: C sharp
Page 31: C sharp
Page 32: C sharp
Page 33: C sharp
Page 34: C sharp
Page 35: C sharp
Page 36: C sharp

2. Rapid Application Development

RAD like Visual Basic and Borland C++ Builder

concise syntax as Java event-driven programming style

link

Page 37: C sharp
Page 38: C sharp
Page 39: C sharp
Page 40: C sharp
Page 41: C sharp
Page 42: C sharp
Page 43: C sharp
Page 44: C sharp
Page 45: C sharp
Page 46: C sharp

3. Use Assembly 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

link

Page 47: C sharp

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

Page 48: C sharp
Page 49: C sharp
Page 50: C sharp
Page 51: C sharp
Page 52: C sharp

4. Namespaces Namespace prevent name conflicts

A namespace can be split over several files separated within the same files

Namespaces can be nested

Page 53: C sharp

Example 1 A namespace split over several files link

Page 54: C sharp
Page 55: C sharp

Example 2 Namespaces Prevent Name Conflicts link

The same class name

The same method name

Page 56: C sharp

Example 3 Namespaces Can Be Nested link

Page 57: C sharp
Page 58: C sharp

5. Properties Question:

How do you access private data in Java?

Properties provide the illusion that we can use private data directly

Page 59: C sharp

example:

Property

Page 60: C sharp

Example 1 link

Page 61: C sharp

6. Indexers An indexer allows an object to be indexed like an array

example:

Page 62: C sharp

Example 1 link

Page 63: C sharp

set

get

Page 64: C sharp

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

Page 65: C sharp

An object 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

Page 66: C sharp

Example 1 linkCall instance method

Page 67: C sharp

Instance method

Page 68: C sharp

Example 2 Call class static method link

Page 69: C sharp

class static method

Page 70: C sharp

Example 3 Delegate supports multicasting link

Page 71: C sharp
Page 72: C sharp
Page 73: C sharp

Example 4 Simple Event & Delegate Demo link

Delegate

Class static method

Page 74: C sharp

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

Page 75: C sharp

Example 1 RTTI Demo link

Page 76: C sharp
Page 77: C sharp

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

Page 78: C sharp

MemberInfo[ ] Getmembers()

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

Page 79: C sharp

Example 1 Obtain class method link

Page 80: C sharp

Example 2 Obtain class constructor link

Page 81: C sharp

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

Page 82: C sharp
Page 83: C sharp
Page 84: C sharp

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

link

Step 1

Page 85: C sharp

No Main()

1

2

3

Page 86: C sharp

Step 2

1

2

Page 87: C sharp

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.

Page 88: C sharp

Example 1 linkSimple pointer demo

Page 89: C sharp

Right-click the mouse

1

2

3

4

Page 90: C sharp

Example 2 Using fixedt should be fixed in one location while p was point to &t.num

Managed object

link

Page 91: C sharp

11. The object class base class of all C# classes object is just another name for System.Object used in Boxing & Unboxing

Boxing : an object reference refers to a value type Unboxing : retrieve a value from a object

as a Generic Data type

Page 92: C sharp

Example 1 Boxing and Unboxing link

Page 93: C sharp

Example 2 object as a generic data type link

Page 94: C sharp

12. ref & out parameter passing

Value Type : pass by value Reference Type : pass by reference

ref & out let you pass value type by reference

Page 95: C sharp

Example 1 ref & out link

Page 96: C sharp

Example 2 swap with ref link

Page 97: C sharp

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

Page 98: C sharp

Example 1 Access bass class’s private data through properties

link

Page 99: C sharp

Example 2 Calling Base Class Constructor link

Page 100: C sharp
Page 101: C sharp

Example 3 Inheritance and Name Hiding link

Page 102: C sharp
Page 103: C sharp

Example 4 Using base to access a hidden item link

Page 104: C sharp

Example 5 Virtual Methods and Overriding (polymorphism and dynamic binding)

link

Page 105: C sharp
Page 106: C sharp
Page 107: C sharp

Example 6 Using sealed to prevent inheritance link

Page 108: C sharp
Page 109: C sharp

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.

Page 110: C sharp

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

Page 111: C sharp

Example 1 Interface Properties link

Page 112: C sharp

Example 2 Interface Indexers link

Page 113: C sharp
Page 114: C sharp

Example 3 Interface can be inherited link

Page 115: C sharp

Example 4 Interface Polymorphism link

Page 116: C sharp

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

Page 117: C sharp

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.

Page 118: C sharp

Example 1 Structure Demo link

Page 119: C sharp
Page 120: C sharp

Programming withProgramming with

C# and .NETC# and .NET

.NET Remoting.NET Remoting

Page 121: C sharp

Outline

Introduction

Basic Architecture

Examples

2

3

1

4 Conclusion

Page 122: C sharp

Introduction

Page 123: C sharp

1. Basic Model

Proxy

Formatter

Client Channel

Remote Object

Formatter

Server Channel

Client Server

2

3

4

5

6

7

8

1 9

Page 124: C sharp

2. Calling Procedure Client Side

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

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

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

1

2

3

4

Page 125: C sharp

Server Side

The server channel sends the serialized data to a formatter.

The formatter deserialized the message.

The client channel communicates with the server chan

nel to transfer the message across the network.

5

6

7

8 The deserialized messages are then dispatched to the remote object.

Page 126: C sharp

3. Configuration Option

Well-known

Remote ObjctSingleton

SingleCall

Client-activated

Page 127: C sharp

Binary

Formatter

SOAP

HTTP

Channel

TCP

Page 128: C sharp

4. Related Technology

1

2

3

DCOM

Java RMI

CORBA

Page 129: C sharp

Architecture & Examples

Page 130: C sharp

Architecture OverviewRemote Objects

Well-known: SingleCall vs. Singleton Client-activated

Activation Server-activated object (SAO) Client-activated object (CAO)

Channel TCP HTTP

Page 131: C sharp

Formatter Binary SOAP

Proxy Transparent Real

Marshaling Marshal-by-value (MBV) Marshal-by-reference (MBR)

Page 132: C sharp

Lease-Base Lifetime lease.RenewOnCallTime sponsor.RenewalTime

Lease machanism is for long-lived objects

1. Well-known singleton

2. Client-activated

SingleCall types do not participate in the lifetime lease system.

Page 133: C sharp

1. SingleCall

Remote object

Remote object

Remote object

Client Server

Page 134: C sharp

Did not cause the server to create a remote object

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

Page 135: C sharp

Server

??

Page 136: C sharp

Every remote method call will create 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

Page 137: C sharp

Example 1 programmatic configuration link

Page 138: C sharp

Assembly

Endpoint

Page 139: C sharp
Page 140: C sharp

Example 2 with configuration file link

Page 141: C sharp
Page 142: C sharp
Page 143: C sharp
Page 144: C sharp
Page 145: C sharp

2. Singleton

Remote object

Client Server

Page 146: C sharp

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.

Page 147: C sharp

Server

Page 148: C sharp

The first remote method call will create a remote object on the server

Multiple clients be serviced by only one remote object

be careful about the concurrency and data protection problem

use lease-based lifetime mechanism

Page 149: C sharp

3. Client-activated

Remote object

Client Server

Page 150: C sharp

Cause the server to create a remote object

Did not create any more remote object

Page 151: C sharp

Server

Page 152: C sharp

Example 1 programmatic configuration link

Page 153: C sharp
Page 154: C sharp

RemoteObject Constructor Argument

Page 155: C sharp

Example 2 with configuration file link

Page 156: C sharp
Page 157: C sharp
Page 158: C sharp
Page 159: C sharp
Page 160: C sharp

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.

Page 161: C sharp

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.

Page 162: C sharp

Remote object

Client Server

RenewOnCallTime

CurrentLeaseTime

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

else {CurrentLeaseTime= RenewOnCallTime;}

Page 163: C sharp

Remote object

Client Server

Leasing time expired

Client-activated

Cause a exception

Page 164: C sharp
Page 165: C sharp

Client Server

Leasing time expired

Remote object

Well-known singleton

Remote object

Create a new remote object

Page 166: C sharp
Page 167: C sharp

Example 1 CAO lease.RenewOnCallTime link

Page 168: C sharp

Example 2 sponsor link

Page 169: C sharp
Page 170: C sharp

5. Marshal-By-Value

a

Client Server

a

Page 171: C sharp

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]

Page 172: C sharp

Example 1 return object with MBV link

Page 173: C sharp
Page 174: C sharp

Client side

Page 175: C sharp

6. Marshal-By-Reference Client Server

a

proxy

Page 176: C sharp

MBR creates a proxy on the client that is used to communicate with the remote object

The class must derived from MarshalByRefObject

Page 177: C sharp

Example 1 return object with MBR link

Page 178: C sharp
Page 179: C sharp

Server side

Page 180: C sharp

Conclusion

Page 181: C sharp

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.