CSharp Tutorial
CSharp Tutorial
C# and .NET
Outline
1 Demo
2 .NET introduction
3 C# Programming
4 .NET Remoting
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
Image Processing
OpenGL Programming(3D Graphics)
DirectX Programming
SEE DEMO
Programming with
C# and .NET
.NET
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
.NET Framework Overview
Applications written in J#
.NET, VB .NET, or C#
FCL
CLR
Windows API
Windows Operating System
(Windows ME, 98, 2000, XP etc)
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
.NET
C# VB .NET Visual
J# .NET
Compile
into MSIL
MSIL
CLR do this
Java Byte
Code
JVM do this
CLR generated a
single application
(native code)
Windows
native code
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
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
.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
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
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
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
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
CLR vs. CLI
ASP .NET
host on IIS server
.NET Remoting
can host on any type of applications
Programming with
Windows Programming
C# and .NET
so easy!
C#
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.
Special features
Properties
Indexers
Delegates and Events
Operator Overloading
Reflection
Attributes
Formatting
Regular Expression
Pointer
Miscellaneous features
jagged array
foreach loop
Runtime type identification (RTTI)
goto
structure (not the same with C/C++)
Start
Programming
1. Simple Console Program link
namespace method
class
Property
Example 1 link
6. Indexers
An indexer allows an object to be indexed
like an array
example:
Example 1 link
set
get
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
An object that has an interest in an event re
gisters an handler for that event
When the event occurs, all registered han
dlers are called.
Event handlers are represented by delega
te.
You can use event accessors to change the
way the event handlers are add to or remo
ve from the invocation list
Example 1 Call instance method link
Instance
method
Example 2 Call class static method link
class static method
Example 3 Delegate supports multicasting link
Example 4 Simple Event & Delegate Demo link
Delegate
8. RTTI
Runtime type identification(RTTI) allows t
he type of an object to be determined during prog
ram execution.
There are three keywords support RTTI
is
as
typeof
Example 1 RTTI Demo link
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
MemberInfo[ ] Getmembers()
MethodInfo[ ] GetMethods()
PropertyInfo[ ] GetProperties()
Example 1 Obtain class method link
Example 2 Obtain class constructor link
Example 3 Obtain Types from Assemblies link
MyClass.cs
3
No Main()
Step 2
1
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.
Example 1 Simple pointer demo link
Right-click the mouse
3
4
2
Example 2 Using fixed link
t should be fixed in
one location while p
was point to &t.num
Managed
object
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
Example 1 Boxing and Unboxing link
Example 2 object as a generic data type link
12. ref & out
parameter passing
Value Type : pass by value
Reference Type : pass by reference
ref & out let you pass value type by reference
Example 1 ref & out link
Example 2 swap with ref link
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
Example 1 Access bass class’s private data link
through properties
Example 2 Calling Base Class Constructor link
Example 3 Inheritance and Name Hiding link
Example 4 Using base to access a hidden item link
Example 5 Virtual Methods and Overriding link
(polymorphism and dynamic binding)
Example 6 Using sealed to prevent inheritance link
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.
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
Example 1 Interface Properties link
Example 2 Interface Indexers link
Example 3 Interface can be inherited link
Example 4 Interface Polymorphism link
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
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.
Example 1 Structure Demo link
Programming with
C# and .NET
.NET Remoting
Outline
1 Introduction
2 Basic Architecture
3 Examples
4 Conclusion
Introductio
n
1. Basic Model
Client Server
Proxy 1 Remote Object 9
2 8
3 Formatter Formatter 7
4 6
Server Side
SingleCall
Well-known
Singleton
Remote Objct
Client-activated
SOAP
Formatter
Binary
TCP
Channel
HTTP
4. Related Technology
1 DCOM
2 Java RMI
3 CORBA
Architecture & Ex
amples
Architecture Overview
Remote Objects
Well-known: SingleCall vs. Singleton
Client-activated
Activation
Server-activated object (SAO)
Client-activated object (CAO)
Channel
TCP
HTTP
Formatter
Binary
SOAP
Proxy
Transparent
Real
Marshaling
Marshal-by-value (MBV)
Marshal-by-reference (MBR)
Lease-Base Lifetime
lease.RenewOnCallTime
sponsor.RenewalTime
Remote object
Remote object
Remote object
Did not cause the server to creat
e a remote object
??
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
Example 1 programmatic configuration link
Assembly
Endpoint
Example 2 with configuration file link
2. Singleton
Client Server
Remote object
Did not cause the server to creat
e a remote object
Remote object
Cause the server to create a rem
ote object
Client Server
Remote object
Leasing time expired
Cause a exception
Well-known singleton
Client Server
Remote object
Leasing time expired
Remote object
a
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]
Example 1 return object with MBV link
Client side
6. Marshal-By-Reference
Client Server
proxy
MBR creates a proxy on the client that is
used to communicate with the remote
object
The class must derived from
MarshalByRefObject
Example 1 return object with MBR link
Server side
Conclusion
Conclusion