SlideShare a Scribd company logo
Programming with   C# and .NET
Outline Demo .NET  introduction C#  Programming 2 3 1 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
CLR C ommon  L anguage  R untime a runtime environment concept similar to  JVM FCL  F ramework  C lass  L ibrary built on top of the CLR provide services for modern applications Major Components
Applications written in J# .NET, VB .NET, or C# .NET Framework Overview CLR FCL Windows Operating System (Windows ME, 98, 2000, XP etc) Windows API
MSIL M icro s oft  I ntermediate  L anguage a  CPU independent  set of instructions .NET compliant language  compile into MSIL similar to  Java Byte Code sometimes abbreviated as  IL
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
Java Java Byte Code Linux native code Java Windows native code Mac OS native code JVM  do this
.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 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
C# VB .NET Visual J# .NET Interoperability Windows native code Compile into MSIL linked the MSIL codes CLR  generated a  single  application (native code) MSIL MSIL MSIL
Rules defined in C ommon  T ype  S ystem ( CTS ) C ommon  L anguage  S pecification ( CLS ) Cross-language development Cross-language  Debugging Cross-language  Exception Handling Cross-language  Inheritance Language Interoperability
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
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
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
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
Web Services  ASP .NET host on IIS server .NET Remoting  can host on any type of applications
Programming with   C# and .NET C# Windows Programming so easy!
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  Visual Studio .NET IDE introduction C# program overview System.Console.WriteLine(…); Build and Run C# program namespace class method link
 
 
 
 
 
 
2. Rapid Application Development  RAD like Visual Basic and Borland C++ Builder concise syntax as Java event-driven programming style link
 
 
 
 
 
 
 
 
 
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
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
 
 
 
 
4. Namespaces  Namespace prevent name conflicts   A namespace can be  split over several files separated within the same files  Namespaces can be nested
Example 1 A namespace split over several files link
 
Example 2 Namespaces Prevent Name Conflicts link The same class name The same method name
Example 3 Namespaces Can Be Nested link
 
5. Properties  Question:  How do you access private data in Java? Properties provide the illusion that we can use  private  data directly
example: 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  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
Example 1 link Call instance method
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 Class static method
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
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 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
 
 
Example 4 How to create and use a DLL with MS Visual Studio .NET  link Step 1
No Main() 1 2 3
Step 2 1 2
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 link Simple pointer demo
Right-click the mouse 1 2 3 4
Example 2 Using fixed t should be fixed in one location while p was point to &t.num Managed object link
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 through properties link
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 (polymorphism and dynamic binding) link
 
 
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 Introduction Basic Architecture Examples 2 3 1 4 Conclusion
Introduction
1. Basic Model  Proxy Formatter Client Channel Remote Object Formatter Server Channel Client Server 2 3 4 5 6 7 8 1 9
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
Server Side The server channel  sends the serialized data to a formatter . The formatter  deserialized the message . The client channel communicates with the server channel to  transfer the message across the network . 5 6 7 8 The deserialized messages are then  dispatched to the remote object .
3. Configuration Option  Well-known Remote Objct Singleton SingleCall Client-activated
Binary Formatter SOAP HTTP Channel TCP
4. Related Technology  1 2 3 DCOM Java RMI CORBA
Architecture   &   Examples
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 Lease machanism is for long-lived objects Well-known singleton Client-activated SingleCall   types do not participate in the lifetime lease system.
1. SingleCall  Remote object Remote object Remote object Client Server
Did not  cause the server to create  a remote object each method call  caused the server to create a new remote object
Server ??
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  Remote object Client Server
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.
Server
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
3. Client-activated  Remote object Client Server
Cause the server to  create  a remote object Did not  create any more remote object
Server
Example 1 programmatic configuration  link
 
RemoteObject Constructor Argument
Example 2 with configuration file link
 
 
 
 
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.
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.
Remote object Client Server RenewOnCallTime CurrentLeaseTime If (CurrentLeaseTime >= RenewOnCallTime) {//Do nothing} else {CurrentLeaseTime= RenewOnCallTime;}
Remote object Client Server Leasing time expired Cause a exception Client-activated
 
Client Server Leasing time expired Remote object Create a new remote object Remote object Well-known singleton
 
Example 1 CAO lease.RenewOnCallTime  link
Example 2 sponsor  link
 
5. Marshal-By-Value  a Client Server 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 a 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  .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.

More Related Content

PPTX
Introduction To C#
PPTX
C# language
PPTX
CSharp Presentation
PPTX
C# Tutorial
PDF
TypeScript
PPTX
C sharp
PPSX
Introduction to .net framework
PPT
TypeScript Presentation
Introduction To C#
C# language
CSharp Presentation
C# Tutorial
TypeScript
C sharp
Introduction to .net framework
TypeScript Presentation

What's hot (20)

PPTX
Python/Flask Presentation
PPTX
Typescript in 30mins
PDF
NodeJS for Beginner
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPTX
Typescript ppt
PPT
Introduction To C#
PDF
Intellij idea tutorial
PPTX
Basic Concept of Node.js & NPM
PPTX
C# 101: Intro to Programming with C#
PPTX
Introduction to C# Programming
PPSX
C# - Part 1
PPT
7.data types in c#
PPTX
C# in depth
PDF
Py.test
PPT
C# Basics
PDF
C# Dot net unit-2.pdf
PPTX
OOPS Basics With Example
PPT
ADO.NET Entity Framework
PPT
C# basics
PPTX
Introduction to c#
Python/Flask Presentation
Typescript in 30mins
NodeJS for Beginner
Basic Concepts of OOPs (Object Oriented Programming in Java)
Typescript ppt
Introduction To C#
Intellij idea tutorial
Basic Concept of Node.js & NPM
C# 101: Intro to Programming with C#
Introduction to C# Programming
C# - Part 1
7.data types in c#
C# in depth
Py.test
C# Basics
C# Dot net unit-2.pdf
OOPS Basics With Example
ADO.NET Entity Framework
C# basics
Introduction to c#
Ad

Viewers also liked (20)

PDF
C sharp programming[1]
PPTX
C Sharp Crash Course
PPT
Introduction to c_sharp
DOC
Visual c sharp
PPTX
introduction to c #
PDF
dotnet_remoting
PPTX
14 Programación Web con .NET y C#
PPT
Top 9 Features Of a Successful Android Application
PPT
Session 9
DOC
Serialization in .NET
PPTX
C Sharp Course 101.5
PPT
Session 6
PPT
Dot NET Remoting
PPTX
Net remoting
PPTX
The .net remote systems
PDF
Overview of Microsoft .Net Remoting technology
PDF
Object oriented-programming-in-c-sharp
PDF
MVC Seminar Presantation
PPTX
.Net framework
ODP
Why Use MVC?
C sharp programming[1]
C Sharp Crash Course
Introduction to c_sharp
Visual c sharp
introduction to c #
dotnet_remoting
14 Programación Web con .NET y C#
Top 9 Features Of a Successful Android Application
Session 9
Serialization in .NET
C Sharp Course 101.5
Session 6
Dot NET Remoting
Net remoting
The .net remote systems
Overview of Microsoft .Net Remoting technology
Object oriented-programming-in-c-sharp
MVC Seminar Presantation
.Net framework
Why Use MVC?
Ad

Similar to C sharp (20)

DOCX
C# Unit 1 notes
PPT
Introduction to Visual Studio.NET
PPTX
.Net slid
PPT
Intro.net
PPTX
Session2 (3)
PPT
Basics of c# by sabir
PPT
Microsoft.Net
PDF
C# c# for beginners crash course master c# programming fast and easy today
PPT
C#_01_CLROverview.ppt
PDF
Dotnet interview qa
PPT
.Net Session Overview
PDF
Dot net
PPT
CSharp_01_CLROverview_and Introductionc#
PPT
C Course Material0209
PPT
Nakov - .NET Framework Overview - English
PPSX
Introductionto .netframework by Priyanka Pinglikar
PPT
1.Philosophy of .NET
PPTX
.Net framework
PPTX
1. Introduction to C# Programming Langua
C# Unit 1 notes
Introduction to Visual Studio.NET
.Net slid
Intro.net
Session2 (3)
Basics of c# by sabir
Microsoft.Net
C# c# for beginners crash course master c# programming fast and easy today
C#_01_CLROverview.ppt
Dotnet interview qa
.Net Session Overview
Dot net
CSharp_01_CLROverview_and Introductionc#
C Course Material0209
Nakov - .NET Framework Overview - English
Introductionto .netframework by Priyanka Pinglikar
1.Philosophy of .NET
.Net framework
1. Introduction to C# Programming Langua

More from Satish Verma (6)

PPT
Visual Studio .NET2010
PPT
Tutorial csharp
PPT
Introduction to csharp
PPT
Gu iintro(java)
PPT
(02) c sharp_tutorial
PPT
Visual studio.net
Visual Studio .NET2010
Tutorial csharp
Introduction to csharp
Gu iintro(java)
(02) c sharp_tutorial
Visual studio.net

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Spectroscopy.pptx food analysis technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation theory and applications.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Electronic commerce courselecture one. Pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
cuic standard and advanced reporting.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Big Data Technologies - Introduction.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
MYSQL Presentation for SQL database connectivity
Network Security Unit 5.pdf for BCA BBA.
Understanding_Digital_Forensics_Presentation.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Spectroscopy.pptx food analysis technology
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation theory and applications.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Electronic commerce courselecture one. Pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Programs and apps: productivity, graphics, security and other tools
cuic standard and advanced reporting.pdf
Machine learning based COVID-19 study performance prediction
Advanced methodologies resolving dimensionality complications for autism neur...
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation_ Review paper, used for researhc scholars
Big Data Technologies - Introduction.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
MYSQL Presentation for SQL database connectivity

C sharp

  • 1. Programming with C# and .NET
  • 2. Outline Demo .NET introduction C# Programming 2 3 1 4 .NET Remoting
  • 3. Programming with C# and .NET Demo
  • 4. 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
  • 5. Programming with C# and .NET .NET
  • 6. CLR C ommon L anguage R untime a runtime environment concept similar to JVM FCL F ramework C lass L ibrary built on top of the CLR provide services for modern applications Major Components
  • 7. Applications written in J# .NET, VB .NET, or C# .NET Framework Overview CLR FCL Windows Operating System (Windows ME, 98, 2000, XP etc) Windows API
  • 8. MSIL M icro s oft I ntermediate L anguage a CPU independent set of instructions .NET compliant language compile into MSIL similar to Java Byte Code sometimes abbreviated as IL
  • 9. 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
  • 10. Java Java Byte Code Linux native code Java Windows native code Mac OS native code JVM do this
  • 11. .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 , …
  • 12. 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
  • 13. C# VB .NET Visual J# .NET Interoperability Windows native code Compile into MSIL linked the MSIL codes CLR generated a single application (native code) MSIL MSIL MSIL
  • 14. Rules defined in C ommon T ype S ystem ( CTS ) C ommon L anguage S pecification ( CLS ) Cross-language development Cross-language Debugging Cross-language Exception Handling Cross-language Inheritance Language Interoperability
  • 15. 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
  • 16. .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
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. Web Services ASP .NET host on IIS server .NET Remoting can host on any type of applications
  • 24. Programming with C# and .NET C# Windows Programming so easy!
  • 25. 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.
  • 26. Special features Properties Indexers Delegates and Events Operator Overloading Reflection Attributes Formatting Regular Expression
  • 27. Pointer Miscellaneous features jagged array foreach loop Runtime type identification (RTTI) goto structure (not the same with C/C++)
  • 29. 1. Simple Console Program Visual Studio .NET IDE introduction C# program overview System.Console.WriteLine(…); Build and Run C# program namespace class method link
  • 30.  
  • 31.  
  • 32.  
  • 33.  
  • 34.  
  • 35.  
  • 36. 2. Rapid Application Development RAD like Visual Basic and Borland C++ Builder concise syntax as Java event-driven programming style link
  • 37.  
  • 38.  
  • 39.  
  • 40.  
  • 41.  
  • 42.  
  • 43.  
  • 44.  
  • 45.  
  • 46. 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
  • 47. 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
  • 48.  
  • 49.  
  • 50.  
  • 51.  
  • 52. 4. Namespaces Namespace prevent name conflicts A namespace can be split over several files separated within the same files Namespaces can be nested
  • 53. Example 1 A namespace split over several files link
  • 54.  
  • 55. Example 2 Namespaces Prevent Name Conflicts link The same class name The same method name
  • 56. Example 3 Namespaces Can Be Nested link
  • 57.  
  • 58. 5. Properties Question: How do you access private data in Java? Properties provide the illusion that we can use private data directly
  • 61. 6. Indexers An indexer allows an object to be indexed like an array example:
  • 64. 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
  • 65. 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
  • 66. Example 1 link Call instance method
  • 67. Instance method
  • 68. Example 2 Call class static method link
  • 70. Example 3 Delegate supports multicasting link
  • 71.  
  • 72.  
  • 73. Example 4 Simple Event & Delegate Demo link Delegate Class static method
  • 74. 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
  • 75. Example 1 RTTI Demo link
  • 76.  
  • 77. 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
  • 78. MemberInfo[ ] Getmembers() MethodInfo[ ] GetMethods() PropertyInfo[ ] GetProperties()
  • 79. Example 1 Obtain class method link
  • 80. Example 2 Obtain class constructor link
  • 81. 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
  • 82.  
  • 83.  
  • 84. Example 4 How to create and use a DLL with MS Visual Studio .NET link Step 1
  • 85. No Main() 1 2 3
  • 86. Step 2 1 2
  • 87. 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 .
  • 88. Example 1 link Simple pointer demo
  • 90. Example 2 Using fixed t should be fixed in one location while p was point to &t.num Managed object link
  • 91. 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
  • 92. Example 1 Boxing and Unboxing link
  • 93. Example 2 object as a generic data type link
  • 94. 12. ref & out parameter passing Value Type : pass by value Reference Type : pass by reference ref & out let you pass value type by reference
  • 95. Example 1 ref & out link
  • 96. Example 2 swap with ref link
  • 97. 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
  • 98. Example 1 Access bass class’s private data through properties link
  • 99. Example 2 Calling Base Class Constructor link
  • 100.  
  • 101. Example 3 Inheritance and Name Hiding link
  • 102.  
  • 103. Example 4 Using base to access a hidden item link
  • 104. Example 5 Virtual Methods and Overriding (polymorphism and dynamic binding) link
  • 105.  
  • 106.  
  • 107. Example 6 Using sealed to prevent inheritance link
  • 108.  
  • 109. 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 .
  • 110. 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
  • 111. Example 1 Interface Properties link
  • 112. Example 2 Interface Indexers link
  • 113.  
  • 114. Example 3 Interface can be inherited link
  • 115. Example 4 Interface Polymorphism link
  • 116. 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
  • 117. 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 .
  • 118. Example 1 Structure Demo link
  • 119.  
  • 120. Programming with C# and .NET .NET Remoting
  • 121. Outline Introduction Basic Architecture Examples 2 3 1 4 Conclusion
  • 123. 1. Basic Model Proxy Formatter Client Channel Remote Object Formatter Server Channel Client Server 2 3 4 5 6 7 8 1 9
  • 124. 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
  • 125. Server Side The server channel sends the serialized data to a formatter . The formatter deserialized the message . The client channel communicates with the server channel to transfer the message across the network . 5 6 7 8 The deserialized messages are then dispatched to the remote object .
  • 126. 3. Configuration Option Well-known Remote Objct Singleton SingleCall Client-activated
  • 127. Binary Formatter SOAP HTTP Channel TCP
  • 128. 4. Related Technology 1 2 3 DCOM Java RMI CORBA
  • 129. Architecture & Examples
  • 130. Architecture Overview Remote Objects Well-known : SingleCall vs. Singleton Client-activated Activation Server-activated object (SAO) Client-activated object (CAO) Channel TCP HTTP
  • 131. Formatter Binary SOAP Proxy Transparent Real Marshaling Marshal-by-value (MBV) Marshal-by-reference (MBR)
  • 132. Lease-Base Lifetime lease.RenewOnCallTime sponsor.RenewalTime Lease machanism is for long-lived objects Well-known singleton Client-activated SingleCall types do not participate in the lifetime lease system.
  • 133. 1. SingleCall Remote object Remote object Remote object Client Server
  • 134. Did not cause the server to create a remote object each method call caused the server to create a new remote object
  • 136. 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
  • 137. Example 1 programmatic configuration link
  • 139.  
  • 140. Example 2 with configuration file link
  • 141.  
  • 142.  
  • 143.  
  • 144.  
  • 145. 2. Singleton Remote object Client Server
  • 146. 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.
  • 147. Server
  • 148. 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
  • 149. 3. Client-activated Remote object Client Server
  • 150. Cause the server to create a remote object Did not create any more remote object
  • 151. Server
  • 152. Example 1 programmatic configuration link
  • 153.  
  • 155. Example 2 with configuration file link
  • 156.  
  • 157.  
  • 158.  
  • 159.  
  • 160. 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.
  • 161. 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.
  • 162. Remote object Client Server RenewOnCallTime CurrentLeaseTime If (CurrentLeaseTime >= RenewOnCallTime) {//Do nothing} else {CurrentLeaseTime= RenewOnCallTime;}
  • 163. Remote object Client Server Leasing time expired Cause a exception Client-activated
  • 164.  
  • 165. Client Server Leasing time expired Remote object Create a new remote object Remote object Well-known singleton
  • 166.  
  • 167. Example 1 CAO lease.RenewOnCallTime link
  • 169.  
  • 170. 5. Marshal-By-Value a Client Server a
  • 171. 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]
  • 172. Example 1 return object with MBV link
  • 173.  
  • 175. 6. Marshal-By-Reference Client Server a proxy
  • 176. MBR creates a proxy on the client that is used to communicate with the remote object The class must derived from MarshalByRefObject
  • 177. Example 1 return object with MBR link
  • 178.  
  • 181. 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.