SlideShare a Scribd company logo
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
1 
C# for C++ Programmers
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
2 
How C# Looks
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
3 
using genericCar; 
namespace Toyota; 
{ 
public class Camry : Car 
{ 
private static Camry camry; 
private Brake brake; 
public void main() 
{ 
camry = new Camry(); 
camry.getBrake().ToString(); 
} 
public Camry() 
{ 
this.brake = new Brake(); 
} 
public override Brake getBrake() 
{ 
return this.brake; 
} 
} 
} 
C# Console Application Project
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
4 
using System.IO; 
namespace Toyota 
{ 
public class Brake 
{ 
string brakeName; 
public Brake() 
{ 
this.brakeName = “Brake 1”; 
} 
public override string ToString() 
{ 
System.Console.Out.Writeln(“I’m ” + this.brakeName); 
} 
} 
} 
Namespace GenericCar 
Class Car 
Public MustOverride Sub getBrake() 
End Class 
C# Class Library Project 
Vb.net Class Library Project
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
5 
Differences between C# and C++
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
6 
Pointers 
• Can be used in C# , but only in code blocks, methods, or classes 
marked with the unsafe keyword. 
• Primarily used for accessing Win32 functions that use pointers. 
class MyClass 
{ 
unsafe int *pX; 
unsafe int MethodThatUsesPointers() 
{ //can use pointers 
} 
int Method() 
{ 
unsafe 
{ //can use pointers 
} 
} 
}
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
7 
References, Classes, and Structs 
• References 
– are used in C#, which are effectively opaque pointers that don’t 
allow the aspects of pointer functionality that can cause bugs. 
• Classes and structs 
– are different in C#: 
– structs are value types, stored on the stack, and cannot inherit, 
– classes always reference types stored on the managed heap 
and are always derivatives of System.Object.
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
8 
Accessing native code 
• C# code can only access native code through PInvoke. 
class PInvoke 
{ 
[DllImport("user32.dll")] 
public static extern int MessageBoxA( int h, string m, string c, int type); 
public static int Main() 
{ 
return MessageBoxA(0, "Hello World!", "My Message Box", 0); 
} 
}
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
9 
Destruction 
• Destruction 
– Same syntax as for C++, but don’t need to declare it as virtual 
and shouldn’t add an access modifier. 
– C# cannot guarantee when a destructor will be called – called by 
the garbage collector. 
– Can force cleanup: System.GC.Collect(); 
– For deterministic destruction, classes should implement 
IDisposable.Dispose() 
– C# supports special syntax that mimics C++ classes that are 
instantiated on the stack where the destructor is called when it 
goes out of scope: 
using (MyClassThatHasDestructor mc = new MyClassThatHasDestructor) 
{ 
//code that uses mc 
} // mc.Dispose() implicitly called when leaving block.
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
10 
Miscellaneous 
• Binary PE format 
– C# compiler will only generate managed assemblies – no native 
code. 
• Operator Overloading 
– C# can overload operators, but not as many. Not commonly 
used. 
• Preprocessor 
– C# has preprocessor directives, similar to C++ but far fewer. No 
separate preprocessor – compiler does preprocessing. 
– C# doesn’t need #include – no need to declare compiler symbols 
used in code but not yet defined.
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
11 
Miscellaneous 
• C# doesn’t require a semicolon after a class 
• C# doesn’t support class objects on the stack. 
• const only at compile time, readonly set once at 
runtime in constructor. 
• C# has no function pointers – delegates instead.
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
12 
C# New Features
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
13 
Delegates 
delegate void AnOperation(int x); 
class AClass 
{ 
void AMethod(int i) 
{ 
System.Console.WriteLine(“Number is ” + i.ToString()); 
} 
static int Main(string[] args) 
{ 
AClass aClass = new AClass(); 
AnOperation anOperation = new AnOperation(AClass.AMethod); 
anOperation(4); 
} 
} 
stdio console output: 
Number is 4 
– When delegate returns a void, is a ‘multicast’ delegate and can represent more than one 
method. += and -= can be used to add and remove a method from a multicast delegate.
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
14 
Events 
• An event source EventSourceClass declares an event 
with this special syntax: 
public delegate void EventClass(obj Sender, EventArgs e); 
public event EventClass SourceEvent; 
• Client event handlers must look like this: 
void MyEventCallback(object sender, EventArgs e) 
{ 
//handle event 
} 
• Client event handlers are added to an event source like 
this: 
EventSourceClass.SourceEvent += MyEventCallback; 
• An event source then invokes the event like this: 
SourceEvent(this, new EventArgs());
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
15 
Attributes and Properties 
• Attributes 
– meta info that can be accessed at runtime 
[WebMethod] 
public ShippingPreference[] GetShippingPreferences(ShoppingCart 
shoppingCart, CustomerInformation customerInformation) 
{ 
} 
• Properties 
class ContainsProperty 
{ 
private int age; 
public int Age 
{ 
get { return age;} 
set { age = value;} 
} 
}
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
16 
Exceptions 
• Exceptions in C# support a finally block: 
try 
{ 
//normal execution path 
} 
catch (Exception ex) 
{ 
//execution jumps to here if Exception or derivative 
//is thrown in try block. 
} 
finally 
{ 
//ALWAYS executes, either after try or catch clause executes. 
}
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
17 
Miscellaneous 
• Interfaces 
• Threading 
lock() statement (shortcut for System.Threading.Monitor) 
• Boxing 
– Value types are primitives. 
– Value types can be treated as objects 
– Even literal types can be treated as objects. 
string age = 42.ToString(); 
int i = 20; 
object o = i;
C++ features unsupported in C# 
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
18 
• No templates – generics instead (C++/CLI has both) 
• No multiple inheritance for base classes.
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
19 
Configuration
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
20 
XML File Based 
• .NET assemblies use XML-based configuration 
files named ExecutableName.exe.config 
• Registry is not commonly used for .NET 
applications.
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
21 
File Format 
• ExecutableName.exe.config looks like: 
<configuration> 
<system.runtime.remoting> 
<application> 
<channels> 
<channel ref="tcp" port="9091" /> 
</channels> 
</application> 
</system.runtime.remoting> 
<appSettings> 
<!-- SERVICE --> 
<!-- false will cause service to run as console application, true requires installing and running as a Windows service --> 
<add key = "Service.isService" value = "false" /> 
<add key = "Service.eventsourcestring" value = "CCLI Service" /> 
<add key = "Service.servicename" value = "CCLI Service" /> 
<add key = "ServiceAssemblyName" value = "Framework" /> 
<add key = "ServiceClassNameConsole" value = "StarPraise.Core.ConsoleBootstrapper" /> 
<add key = "ServiceClassNameService" value = "StarPraise.Core.ComponentController" /> 
<add key = "Service.strStarting" value = "Service starting..." /> 
<add key = "Service.strStopping" value = "Service Stopping..." /> 
<!-- Configuration to use DefaultLogger - writing straight to file. Only for testing - NOT FOR PRODUCTION USE. --> 
<add key = "Logger.loggerAssembly" value = "Framework" /> 
<add key = "Logger.loggerclassname" value = "StarPraise.Core.DefaultLogger" /> 
… 
</configuration>
Accessing Configuration Values 
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
22 
static protected Logger getInstance() 
{ 
if (Logger.loggerInstance == null) 
{ 
try 
{ 
ObjectHandle obj = Activator.CreateInstance( 
System.Configuration.ConfigurationManager.AppSettings[Logger.loggerAssembly], 
System.Configuration.ConfigurationManager.AppSettings[Logger.loggerclassname]); 
Logger.loggerInstance = (Logger) obj.Unwrap(); 
Logger.loggerInstance.Start(); 
} 
catch (Exception ex) 
{ 
throw new RuntimeException(ex.ToString(), new 
ComponentIdentity("Logger"), "getInstance"); 
} 
}
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
23 
Application Types
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
24 
Types 
There are five primary application 
(‘executable’) projects in Visual Studio: 
• Console Applications 
• Services 
• Forms Applications 
• Web Applications 
• Web Services
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
25 
Form Applications 
• VS.NET creates Main and adds to the form 
application project. 
• Windows Message Pump is encapsulated 
entirely within classes contained in 
System.Windows. 
• System.Windows.Forms.Form is type 
automatically created by VS.NET for forms 
application projects.
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
26 
Forms Applications 
using System; 
using System.Collections.Generic; 
using System.Windows.Forms; 
namespace 
CompassPoint.ECommerce.OrderProcessingServ 
ices 
{ 
static class Program 
{ 
/// <summary> 
/// The main entry point for the 
application. 
/// </summary> 
[STAThread] 
static void Main() 
{ 
Application.EnableVisualStyles(); 
Application.SetCompatibleTextRenderingDefa 
ult(false); 
Application.Run(new 
OrderProcessingWebServiceTest()); 
} 
} 
} 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
namespace 
CompassPoint.ECommerce.OrderProcessingServices 
{ 
public partial class 
OrderProcessingWebServiceTest : Form 
{ 
public OrderProcessingWebServiceTest() 
{ 
InitializeComponent(); 
} 
} 
}
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
27 
Web Applications 
• They are hosted in IIS. 
• A variety of compile options from compile when accessed to pre-compile 
including partial compile in the creamy middle! 
• When webpage.aspx is accessed, 
– IIS delegates to the ASP.NET runtime which 
• creates a runtime object model for the page, 
• Reads the page 
• Returns all markup to IIS to return to browser except 
• Embedded runat=server marked code in the page which it 
runs in the runtime object model environment and returns the 
resulting markup.
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
28 
Web Services 
• When a webservice.asmx is accessed, the 
same thing happens, but SOAP ‘method’ 
invocation requests are redirected to 
methods in the web service marked with a 
special attribute.
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
29 
Web Services 
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[ToolboxItem(false)] 
public class OrderProcessingService : System.Web.Services.WebService, 
IOrderManager 
{ 
[WebMethod] 
public ShippingPreference[] GetShippingPreferences(ShoppingCart 
shoppingCart, CustomerInformation customerInformation) 
{ 
….
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
30 
Web Application and Service 
Configuration 
• As for applications, configuration is stored in an 
XML file. 
• XML file is of same format as for other types of 
applications. 
• For both, the file is named web.config.
© 2007 Compass Point, Inc. 
9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net 
www.compass-point.net 
31 
Discussion

More Related Content

PPTX
C# for C++ programmers
PPTX
Les collections en Java
PPTX
C# Tutorial
PPT
Structure in C
PPT
CSharp.ppt
PPTX
inheritance
PPSX
PPTX
C# 101: Intro to Programming with C#
C# for C++ programmers
Les collections en Java
C# Tutorial
Structure in C
CSharp.ppt
inheritance
C# 101: Intro to Programming with C#

What's hot (20)

PPTX
Inheritance ppt
PDF
Concevoir, développer et sécuriser des micro-services avec Spring Boot
PDF
Object oriented programming With C#
PPTX
This pointer
PPTX
Constructor in java
PDF
Correction examen-java-avancé-1
PPTX
Spring security
PPTX
C c#
PPT
JAVA OOP
PPTX
Java Queue.pptx
PDF
Cours design pattern m youssfi partie 3 decorateur
PDF
Support NodeJS avec TypeScript Express MongoDB
PDF
Cours design pattern m youssfi partie 5 adapter
PPT
Exception handling in java
PDF
spring-api-rest.pdf
PPT
Binary operator overloading
PPT
Abstract class
PDF
Cours java
Inheritance ppt
Concevoir, développer et sécuriser des micro-services avec Spring Boot
Object oriented programming With C#
This pointer
Constructor in java
Correction examen-java-avancé-1
Spring security
C c#
JAVA OOP
Java Queue.pptx
Cours design pattern m youssfi partie 3 decorateur
Support NodeJS avec TypeScript Express MongoDB
Cours design pattern m youssfi partie 5 adapter
Exception handling in java
spring-api-rest.pdf
Binary operator overloading
Abstract class
Cours java
Ad

Viewers also liked (16)

ODP
Ppt of c++ vs c#
PDF
C# Cheat Sheet
PPTX
C++ vs C#
PPTX
difference between c c++ c#
PDF
Css cheat-sheet-v3
PPTX
C# interview
PDF
C# Basics Quick Reference Sheet
PPTX
Abstraction in java
PPTX
Abstract class and Interface
PPT
Java Programming - Abstract Class and Interface
PPTX
Classes And Objects
PPT
C++ classes
PDF
Differences between c and c++
PPT
20. Object-Oriented Programming Fundamental Principles
PDF
8 abstract classes and interfaces
PPT
Chapter 9 Abstract Class
Ppt of c++ vs c#
C# Cheat Sheet
C++ vs C#
difference between c c++ c#
Css cheat-sheet-v3
C# interview
C# Basics Quick Reference Sheet
Abstraction in java
Abstract class and Interface
Java Programming - Abstract Class and Interface
Classes And Objects
C++ classes
Differences between c and c++
20. Object-Oriented Programming Fundamental Principles
8 abstract classes and interfaces
Chapter 9 Abstract Class
Ad

Similar to C# for C++ Programmers (20)

PPTX
Get the Gist: .NET
PPT
Introduction to c_sharp
PPT
Introduction to c_sharp
PDF
Intro to c# (vs. objective c and java)
PDF
Intro to c# (vs. objective c and java)
PPT
David buksbaum a-briefintroductiontocsharp
PPT
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
PPT
Introduction-to-Csharp.ppt
PPT
Introduction-to-Csharp programacion orientada a objetos
PPT
Introduction-to-Csharp.ppt
PPT
Introduction to-csharp
PDF
21UCAC61 C# and .Net Programming.pdf(MTNC)(BCA)
PPT
Csharp dot net
PDF
PPT
IntroToCSharpcode.ppt
PPTX
C#unit4
PPT
C#
PPTX
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
PPTX
C# programming language
PDF
C sharp chap1
Get the Gist: .NET
Introduction to c_sharp
Introduction to c_sharp
Intro to c# (vs. objective c and java)
Intro to c# (vs. objective c and java)
David buksbaum a-briefintroductiontocsharp
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
Introduction-to-Csharp.ppt
Introduction-to-Csharp programacion orientada a objetos
Introduction-to-Csharp.ppt
Introduction to-csharp
21UCAC61 C# and .Net Programming.pdf(MTNC)(BCA)
Csharp dot net
IntroToCSharpcode.ppt
C#unit4
C#
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# programming language
C sharp chap1

Recently uploaded (20)

PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
medical staffing services at VALiNTRY
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
System and Network Administraation Chapter 3
PDF
System and Network Administration Chapter 2
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
ManageIQ - Sprint 268 Review - Slide Deck
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
medical staffing services at VALiNTRY
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PTS Company Brochure 2025 (1).pdf.......
System and Network Administraation Chapter 3
System and Network Administration Chapter 2
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
ISO 45001 Occupational Health and Safety Management System
Understanding Forklifts - TECH EHS Solution
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Which alternative to Crystal Reports is best for small or large businesses.pdf
Softaken Excel to vCard Converter Software.pdf

C# for C++ Programmers

  • 1. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 1 C# for C++ Programmers
  • 2. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 2 How C# Looks
  • 3. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 3 using genericCar; namespace Toyota; { public class Camry : Car { private static Camry camry; private Brake brake; public void main() { camry = new Camry(); camry.getBrake().ToString(); } public Camry() { this.brake = new Brake(); } public override Brake getBrake() { return this.brake; } } } C# Console Application Project
  • 4. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 4 using System.IO; namespace Toyota { public class Brake { string brakeName; public Brake() { this.brakeName = “Brake 1”; } public override string ToString() { System.Console.Out.Writeln(“I’m ” + this.brakeName); } } } Namespace GenericCar Class Car Public MustOverride Sub getBrake() End Class C# Class Library Project Vb.net Class Library Project
  • 5. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 5 Differences between C# and C++
  • 6. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 6 Pointers • Can be used in C# , but only in code blocks, methods, or classes marked with the unsafe keyword. • Primarily used for accessing Win32 functions that use pointers. class MyClass { unsafe int *pX; unsafe int MethodThatUsesPointers() { //can use pointers } int Method() { unsafe { //can use pointers } } }
  • 7. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 7 References, Classes, and Structs • References – are used in C#, which are effectively opaque pointers that don’t allow the aspects of pointer functionality that can cause bugs. • Classes and structs – are different in C#: – structs are value types, stored on the stack, and cannot inherit, – classes always reference types stored on the managed heap and are always derivatives of System.Object.
  • 8. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 8 Accessing native code • C# code can only access native code through PInvoke. class PInvoke { [DllImport("user32.dll")] public static extern int MessageBoxA( int h, string m, string c, int type); public static int Main() { return MessageBoxA(0, "Hello World!", "My Message Box", 0); } }
  • 9. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 9 Destruction • Destruction – Same syntax as for C++, but don’t need to declare it as virtual and shouldn’t add an access modifier. – C# cannot guarantee when a destructor will be called – called by the garbage collector. – Can force cleanup: System.GC.Collect(); – For deterministic destruction, classes should implement IDisposable.Dispose() – C# supports special syntax that mimics C++ classes that are instantiated on the stack where the destructor is called when it goes out of scope: using (MyClassThatHasDestructor mc = new MyClassThatHasDestructor) { //code that uses mc } // mc.Dispose() implicitly called when leaving block.
  • 10. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 10 Miscellaneous • Binary PE format – C# compiler will only generate managed assemblies – no native code. • Operator Overloading – C# can overload operators, but not as many. Not commonly used. • Preprocessor – C# has preprocessor directives, similar to C++ but far fewer. No separate preprocessor – compiler does preprocessing. – C# doesn’t need #include – no need to declare compiler symbols used in code but not yet defined.
  • 11. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 11 Miscellaneous • C# doesn’t require a semicolon after a class • C# doesn’t support class objects on the stack. • const only at compile time, readonly set once at runtime in constructor. • C# has no function pointers – delegates instead.
  • 12. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 12 C# New Features
  • 13. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 13 Delegates delegate void AnOperation(int x); class AClass { void AMethod(int i) { System.Console.WriteLine(“Number is ” + i.ToString()); } static int Main(string[] args) { AClass aClass = new AClass(); AnOperation anOperation = new AnOperation(AClass.AMethod); anOperation(4); } } stdio console output: Number is 4 – When delegate returns a void, is a ‘multicast’ delegate and can represent more than one method. += and -= can be used to add and remove a method from a multicast delegate.
  • 14. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 14 Events • An event source EventSourceClass declares an event with this special syntax: public delegate void EventClass(obj Sender, EventArgs e); public event EventClass SourceEvent; • Client event handlers must look like this: void MyEventCallback(object sender, EventArgs e) { //handle event } • Client event handlers are added to an event source like this: EventSourceClass.SourceEvent += MyEventCallback; • An event source then invokes the event like this: SourceEvent(this, new EventArgs());
  • 15. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 15 Attributes and Properties • Attributes – meta info that can be accessed at runtime [WebMethod] public ShippingPreference[] GetShippingPreferences(ShoppingCart shoppingCart, CustomerInformation customerInformation) { } • Properties class ContainsProperty { private int age; public int Age { get { return age;} set { age = value;} } }
  • 16. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 16 Exceptions • Exceptions in C# support a finally block: try { //normal execution path } catch (Exception ex) { //execution jumps to here if Exception or derivative //is thrown in try block. } finally { //ALWAYS executes, either after try or catch clause executes. }
  • 17. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 17 Miscellaneous • Interfaces • Threading lock() statement (shortcut for System.Threading.Monitor) • Boxing – Value types are primitives. – Value types can be treated as objects – Even literal types can be treated as objects. string age = 42.ToString(); int i = 20; object o = i;
  • 18. C++ features unsupported in C# © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 18 • No templates – generics instead (C++/CLI has both) • No multiple inheritance for base classes.
  • 19. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 19 Configuration
  • 20. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 20 XML File Based • .NET assemblies use XML-based configuration files named ExecutableName.exe.config • Registry is not commonly used for .NET applications.
  • 21. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 21 File Format • ExecutableName.exe.config looks like: <configuration> <system.runtime.remoting> <application> <channels> <channel ref="tcp" port="9091" /> </channels> </application> </system.runtime.remoting> <appSettings> <!-- SERVICE --> <!-- false will cause service to run as console application, true requires installing and running as a Windows service --> <add key = "Service.isService" value = "false" /> <add key = "Service.eventsourcestring" value = "CCLI Service" /> <add key = "Service.servicename" value = "CCLI Service" /> <add key = "ServiceAssemblyName" value = "Framework" /> <add key = "ServiceClassNameConsole" value = "StarPraise.Core.ConsoleBootstrapper" /> <add key = "ServiceClassNameService" value = "StarPraise.Core.ComponentController" /> <add key = "Service.strStarting" value = "Service starting..." /> <add key = "Service.strStopping" value = "Service Stopping..." /> <!-- Configuration to use DefaultLogger - writing straight to file. Only for testing - NOT FOR PRODUCTION USE. --> <add key = "Logger.loggerAssembly" value = "Framework" /> <add key = "Logger.loggerclassname" value = "StarPraise.Core.DefaultLogger" /> … </configuration>
  • 22. Accessing Configuration Values © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 22 static protected Logger getInstance() { if (Logger.loggerInstance == null) { try { ObjectHandle obj = Activator.CreateInstance( System.Configuration.ConfigurationManager.AppSettings[Logger.loggerAssembly], System.Configuration.ConfigurationManager.AppSettings[Logger.loggerclassname]); Logger.loggerInstance = (Logger) obj.Unwrap(); Logger.loggerInstance.Start(); } catch (Exception ex) { throw new RuntimeException(ex.ToString(), new ComponentIdentity("Logger"), "getInstance"); } }
  • 23. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 23 Application Types
  • 24. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 24 Types There are five primary application (‘executable’) projects in Visual Studio: • Console Applications • Services • Forms Applications • Web Applications • Web Services
  • 25. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 25 Form Applications • VS.NET creates Main and adds to the form application project. • Windows Message Pump is encapsulated entirely within classes contained in System.Windows. • System.Windows.Forms.Form is type automatically created by VS.NET for forms application projects.
  • 26. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 26 Forms Applications using System; using System.Collections.Generic; using System.Windows.Forms; namespace CompassPoint.ECommerce.OrderProcessingServ ices { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefa ult(false); Application.Run(new OrderProcessingWebServiceTest()); } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace CompassPoint.ECommerce.OrderProcessingServices { public partial class OrderProcessingWebServiceTest : Form { public OrderProcessingWebServiceTest() { InitializeComponent(); } } }
  • 27. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 27 Web Applications • They are hosted in IIS. • A variety of compile options from compile when accessed to pre-compile including partial compile in the creamy middle! • When webpage.aspx is accessed, – IIS delegates to the ASP.NET runtime which • creates a runtime object model for the page, • Reads the page • Returns all markup to IIS to return to browser except • Embedded runat=server marked code in the page which it runs in the runtime object model environment and returns the resulting markup.
  • 28. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 28 Web Services • When a webservice.asmx is accessed, the same thing happens, but SOAP ‘method’ invocation requests are redirected to methods in the web service marked with a special attribute.
  • 29. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 29 Web Services [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem(false)] public class OrderProcessingService : System.Web.Services.WebService, IOrderManager { [WebMethod] public ShippingPreference[] GetShippingPreferences(ShoppingCart shoppingCart, CustomerInformation customerInformation) { ….
  • 30. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 30 Web Application and Service Configuration • As for applications, configuration is stored in an XML file. • XML file is of same format as for other types of applications. • For both, the file is named web.config.
  • 31. © 2007 Compass Point, Inc. 9434 SW 55th Avenue  Portland OR 97219  Phone: 503.329.1138  info@compass-point.net www.compass-point.net 31 Discussion