1 comp 144 programming language concepts felix hernandez-campos lecture 39: case study: c# and.net...
Post on 20-Dec-2015
215 Views
Preview:
TRANSCRIPT
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
11
Lecture 39: Lecture 39: Case Study: C# and .NETCase Study: C# and .NET
COMP 144 Programming Language ConceptsCOMP 144 Programming Language Concepts
Spring 2002Spring 2002
Felix Hernandez-CamposFelix Hernandez-Campos
April 29April 29
The University of North Carolina at Chapel HillThe University of North Carolina at Chapel Hill
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
22
C# and .NETC# and .NET
• In 2000, Microsoft releases a new language, In 2000, Microsoft releases a new language, C#C#, , heavily influences by Java and C++heavily influences by Java and C++
– Is there anything new from the programming languages Is there anything new from the programming languages point of view?point of view?
• Microsoft is making it the key stone in their new Microsoft is making it the key stone in their new development strategy (development strategy (.NET.NET))
– Big bucks… big evil…Big bucks… big evil…
• Let’s have a brief look at it, so you can put it our Let’s have a brief look at it, so you can put it our resumes or simply laugh at Microsoft, depending on resumes or simply laugh at Microsoft, depending on your point of view about the worldyour point of view about the world
– I’m neutral (yeah, right…)I’m neutral (yeah, right…)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
33
Hello WorldHello World
public class HelloWorld {public class HelloWorld { public static void main(String[] args) { public static void main(String[] args) { System.out.println(“Hello world!"); System.out.println(“Hello world!"); } }}}
class HelloWorld {class HelloWorld { static void Main(string[] args) { static void Main(string[] args) { System.Console.WriteLine(“Hello world!"); System.Console.WriteLine(“Hello world!"); } }}}
• JavaJava
• C#C#
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
44
Motivation for C#Motivation for C#
• .NET.NET– New development New development
framework that promises to framework that promises to simplify Windows simplify Windows programmingprogramming
» COM/DCOM is hard to COM/DCOM is hard to learnlearn
– Heavy on component Heavy on component orientationorientation
– Language independence Language independence run-time systemrun-time system
» Common Language Run-Common Language Run-time (CLR)time (CLR)
Framework Class LibrariesFramework Class Libraries
WindowsWindows
Common Language RuntimeCommon Language Runtime
C# programsC# programs VB .NETVB .NET
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
55
Common Language RuntimeCommon Language Runtime
• It can execute .NET program in an intermediate It can execute .NET program in an intermediate representation, the representation, the Common Language InterfaceCommon Language Interface (CLI)(CLI)
• CLR is designed to work well in a multi-language CLR is designed to work well in a multi-language environmentenvironment
– Java Virtual Machines is rather Java-orientedJava Virtual Machines is rather Java-oriented– CLR is supposed to work well with imperative CLR is supposed to work well with imperative
programming languages (programming languages (e.g.e.g., C, Pascal) and statically , C, Pascal) and statically typed object oriented languages (typed object oriented languages (e.g.e.g., C#, Eiffel), C#, Eiffel)
– Many language have compilers for CLR at different stages Many language have compilers for CLR at different stages of development, including Python, Perl, Scheme, Haskell, of development, including Python, Perl, Scheme, Haskell, Prolog,…Prolog,…
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
66
Motivation for C#Motivation for C#
• Rapid application development (RAD)Rapid application development (RAD)– Visual development tools/languages such as Visual Basic Visual development tools/languages such as Visual Basic
and Delphi, are very popular and usefuland Delphi, are very popular and useful» Remember Java Beans lectureRemember Java Beans lecture
– C# is optimized for such development modelC# is optimized for such development model
• Platform-independencePlatform-independence– CLR and CLICLR and CLI
• Access to platform-native resourcesAccess to platform-native resources– A more direct approach than the one taken by Java Native A more direct approach than the one taken by Java Native
Interface (JNI)Interface (JNI)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
77
C# SyntaxC# SyntaxComparison with JavaComparison with Java
• If/then/elseIf/then/else
int i = 0;int i = 0;if (i == 0) {if (i == 0) { i = 1; i = 1;} else {} else { i = 2; i = 2;}}
int i = 0;int i = 0;if (i == 0) {if (i == 0) { i = 1; i = 1;} else {} else { i = 2; i = 2;}}
JavaJava C#C#
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
88
C# SyntaxC# Syntax
• SwitchSwitch
int i = 0;int i = 0;switch (i) {switch (i) { case 0: case 0:
i = 1; i = 1; break;break;
case 1: case 1: i = 2;i = 2;break;break;
default: default:i = -1; i = -1; break;break;
}}
int i = 0;int i = 0;switch (i) {switch (i) { case 0: case 0:
i = 1;i = 1;break;break;
case 1: case 1:i = 2; i = 2; break;break;
default: default:i = -1; i = -1; break;break;
}}
JavaJava C#C#
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
99
C# SyntaxC# Syntax
• WhileWhile
• Do/WhileDo/While
int i = 0;int i = 0;while (i++ < 10) {while (i++ < 10) {}}
int i = 0;int i = 0;while (i++ < 10) {while (i++ < 10) {}}
JavaJava C#C#
int i = 0;int i = 0;do {do {} while (i++ < 10);} while (i++ < 10);
int i = 0;int i = 0;do {do {} while (i++ < 10);} while (i++ < 10);
JavaJava C#C#
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1010
C# SyntaxC# Syntaxforeachforeach
import java.util.Vector;import java.util.Vector;public static int sum(Vector v) {public static int sum(Vector v) { int sum = 0; int sum = 0; for (int j = 0; j < v.size(); j++) { for (int j = 0; j < v.size(); j++) { Integer i = (Integer)v.elementAt(j); Integer i = (Integer)v.elementAt(j); sum = sum + i.intValue(); sum = sum + i.intValue(); } } return sum; return sum;}}
using System.Collections;using System.Collections;static int SumList(ArrayList theList) {static int SumList(ArrayList theList) { int sum = 0; int sum = 0; foreachforeach (int j (int j inin theList) { theList) { sum = sum + j; sum = sum + j; } } return sum; return sum;}}
JavaJava
C#C#
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1111
C# SyntaxC# Syntax
• Break/ContinueBreak/Continue
• ReturnReturn
int i = 0;int i = 0;while (i++ < 10) {while (i++ < 10) { if (i < 5) continue; if (i < 5) continue; break; break;}}
JavaJava
public void public void returnNothing() {returnNothing() { return; return;}}public int returnOne() {public int returnOne() { return 1; return 1;}}
int i = 0;int i = 0;while (i++ < 10) {while (i++ < 10) { if (i < 5) continue; if (i < 5) continue; break; break;}}
C#C#
public void public void returnNothing() {returnNothing() { return; return;}}public int returnOne() {public int returnOne() { return 1; return 1;}}
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1212
C# SyntaxC# Syntax
• Object instantiationObject instantiation
• Exclusive accessExclusive access
Something s = Something s = new Something();new Something();
JavaJava
synchronized(this) {synchronized(this) { // do something// do something}}
Something s = Something s = new Something();new Something();
C#C#
lock(this) {lock(this) { // do something// do something}}
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1313
C# SyntaxC# Syntaxtry/catch/finallytry/catch/finally
try {try { throw new SampleException(); throw new SampleException();} catch (SampleException ex) {} catch (SampleException ex) {} finally {} finally {}}
try {try { throw new SampleException(); throw new SampleException();} catch (SampleException ex) {} catch (SampleException ex) {} finally {} finally {}}
try {try { throw new SampleException(); throw new SampleException();} catch {} finally {} catch {} finally {}}
JavaJava
C#C#
• catchcatch clause is optional clause is optional
• catchcatch argument is optional argument is optional
• No No throwsthrows keyword keyword
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1414
C# SyntaxC# Syntax
• Class definitionClass definition
• Interface definitionInterface definition
• Interface implementationInterface implementation
class Foo extends Bar {class Foo extends Bar {......}}
JavaJava
interface IFoo extends IBar {interface IFoo extends IBar {......}}
class Foo extends Bar {class Foo extends Bar {......}}
C#C#
interface IFoo : IBar {interface IFoo : IBar {......}}
class Foo implements IFoo, class Foo implements IFoo, IBaz {IBaz {......}}
class Bar: IFoo, IBaz {class Bar: IFoo, IBaz {}}
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1515
Other C# FeaturesOther C# Features
• C# provides Java-style garbage collectionC# provides Java-style garbage collection
• C# implements a Java- and Delphi-style C# implements a Java- and Delphi-style value/reference-type systemvalue/reference-type system
– Variables of primitive types also act like objects (unlike Variables of primitive types also act like objects (unlike Java primitive objects)Java primitive objects)
Integer iobj = new Integer(12);Integer iobj = new Integer(12);System.out.println(iobj.toString());System.out.println(iobj.toString());
Console.WriteLine(12.ToString());Console.WriteLine(12.ToString());
JavaJava
C#C#
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1616
Other C# FeaturesOther C# Features
• EnumerationsEnumerationsenum description: ulong {enum description: ulong {
Good, Good, Bad, Bad, Ugly Ugly
};};
• Properties (forced getter and setters)Properties (forced getter and setters)TextBlock tb;TextBlock tb;
if (tb.backgroundColor == Color.green) { if (tb.backgroundColor == Color.green) {
// "get" is called for comparison// "get" is called for comparison
tb.backgroundColor = Color.red; tb.backgroundColor = Color.red; // "set" is called// "set" is called
} else {} else {
tb.backgroundColor = Color.blue; tb.backgroundColor = Color.blue; // "set“ is called// "set“ is called
}}
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1717
Other C# FeaturesOther C# Features
• Get/setGet/setpublic class TextBlock {public class TextBlock {
// Assume Color is an enum// Assume Color is an enum
private Color _bgColor;private Color _bgColor;
private Color _fgColor;private Color _fgColor;
public Color backgroundColor {public Color backgroundColor { getget { {
return _bgColor;return _bgColor;
}} setset { {
_bgColor = value;_bgColor = value; } } //... and so on...//... and so on... } }
}}
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1818
Other C# FeaturesOther C# Features
• DelegatesDelegates– Safe method reference (solved with interfaces in Java)Safe method reference (solved with interfaces in Java)
delegate int Comparator(object a, object b);delegate int Comparator(object a, object b);
class Quicksort {class Quicksort { static void sort(Comparator c, object[] static void sort(Comparator c, object[] objectsToSort) { objectsToSort) { // ... quicksort logic leading to a comparison// ... quicksort logic leading to a comparison if (c(objectsToSort[left], if (c(objectsToSort[left],
objectsToSort[pivot]) > 0) {objectsToSort[pivot]) > 0) {
// recursive call...// recursive call...
} else { } else { // ...and so on...// ...and so on... } }; } };
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1919
ReferencesReferences
• Perry, Perry, C#, the natural progressionC#, the natural progression– http://www.javaworld.com/javaworld/jw-08-2000/jw-0804http://www.javaworld.com/javaworld/jw-08-2000/jw-0804
-itw-csharp.html-itw-csharp.html
• Johnson, Johnson, C#: A language alternative or just J--?C#: A language alternative or just J--?– http://www.javaworld.com/javaworld/jw-08-2000/jw-0804http://www.javaworld.com/javaworld/jw-08-2000/jw-0804
-itw-csharp.html-itw-csharp.html
• Meijer and Gough, Meijer and Gough, Technical Overview of the Technical Overview of the Common Language RuntimeCommon Language Runtime
– http://research.microsoft.com/~emeijer/Papers/CLR.http://research.microsoft.com/~emeijer/Papers/CLR.pdfpdf
top related