Presentation is loading. Please wait.

Presentation is loading. Please wait.

C#: CS1 Should We Switch ? WG2.4 AUGUST 7,2003 IRA POHL  ALGOL – PL/1 – Pascal – C / C++ / Java / C#  What have we learned from CS1 choices  Neat Features.

Similar presentations


Presentation on theme: "C#: CS1 Should We Switch ? WG2.4 AUGUST 7,2003 IRA POHL  ALGOL – PL/1 – Pascal – C / C++ / Java / C#  What have we learned from CS1 choices  Neat Features."— Presentation transcript:

1 C#: CS1 Should We Switch ? WG2.4 AUGUST 7,2003 IRA POHL  ALGOL – PL/1 – Pascal – C / C++ / Java / C#  What have we learned from CS1 choices  Neat Features of C#  C# vs. Java vs. C++  pohl@cs.ucsc.edu

2 Family of Languages

3 CS1 through the years  Academic computer science in the 60’s  ALGOL 60 vs. FORTRAN  Confusion at the end of the 60’s  Pl/1, ALGOL W, BASIC, ALGOL 68  Pascal consensus

4 CS1: mid-80’s through present  C++ becomes a contender: very influential  Complex, but OO starts being dominant  1995: Java phenomena  2000: Java starts achieving majority status  2002: C# – why it will replace Java  (ALGOL W v Pascal – incremental change)

5 Copyright Ira Pohl 2002 5 ALGOL 60 BLOCKS,TYPE DECLARATION,RECURSION BNF, FOR, CALL BY NAME, PUBLICATION, PROCEDURAL ENCAPSULATION CS IS DOMINATED BY NUMERICAL SCIENCE CHIEF COMPETITION IS FORTRAN

6 Copyright Ira Pohl 2002 6 TROUBLESPOTS Knuth’s 1967 Trouble spots paper: begin integer a; integer procedure f(x, y); value y, x; integer y, x; a := f := x + 1; integer procedure g(x); integer x; x := g := a + 2; a := 0; outreal(1, a + f(a,g(a))/g(a)) end;

7 Copyright Ira Pohl 2002 7 OH PASCAL -1970 NEED NON NUMERICAL TYPES RECORDS PL1 ALGOL W ALGOL68 PASCAL ALGOL W V. PASCAL V. ALGOL 68 CHEAP, WIDESPREAD, INCREMENTALLY BETTER, BOOKS

8 Copyright Ira Pohl 2002 8 MODULE AND ADT 1980 PASCAL AND STEPWISE REFINEMENT WEAK LIBRARIES, NON-MODULAR SO UCSD PASCAL, MODULA2, C, ADA SHOULD WIRTH HAVE DEVELOPED PASCAL II

9 Copyright Ira Pohl 2002 9 OO AND UNIX 1985 LARGER SCALE UNIX PROJECT INFLUENCE STUDENTS WANT JOB SKILLS MAKE AND FILES SMALLTALK OO VISIBILITY INCREASES C, C++, MOULA 2 AND PASCAL, ML

10 Copyright Ira Pohl 2002 10 JAVA 1995-2000 C++ TOO COMPLEX BUT OO NOW DOMINATES C TO PRIMITIVE SCHEME EIFFEL …. NICHE JAVA WORE{WRITE 1 RUN EVERWHERE} APPLETS WEB NO POINTERS PURE OO NEW CONSENSUS

11 CS1 Requirements  Modern and supports dominant methodology  Pascal: structured programming  C#, C++, Java: OOP  Multi-platform,excellent implementation books  Type safety, assertions, error messages  syntax, runtime  In Wide Use (has yet to happen for C#)

12 Traditional First C# Program // Hello world in C# // by Olivia Programmer using System; class Hello { public static void Main() { Console.WriteLine("Hello, world!"); } }

13 “The Alpha Centarians say Hello in C#!”

14 Make change in C++ // Change in dimes and pennies #include using namespace std; int main() { int price, change, dimes, pennies; cout > price; change = 100 - price; // how much change dimes = change / 10; // number of dimes pennies = change % 10; // number of pennies cout << "\n\nThe change is :" << dimes << " dimes "; cout << pennies << " pennies." << endl; }

15 MakeChange Java by Dissection import tio.*; // use the package tio class MakeChange { public static void main(String[] args) { int price, change, dimes, pennies; System.out.println("type price (0:100):"); price = Console.in.readInt(); change = 100 - price; // how much change dimes = change / 10; // number of dimes pennies = change % 10; // number of pennies System.out.print("The change is :"); System.out.print(dimes); System.out.print(" dimes "); System.out.print(pennies); System.out.print(" pennies.\n"); } }

16 C# by Dissection chapter 1 example using System; class MakeChange { public static void Main() { int price, change, dimes, pennies; // declarations string data; Console.WriteLine("Enter price (0:100):"); data = Console.ReadLine(); price = int.Parse(data); change = 100 - price; // how much change dimes = change / 10; // number of dimes pennies = change % 10; // number of pennies Console.Write("\n\nThe change is: " + dimes + " dimes "); Console.WriteLine(pennies + " pennies."); } }

17 C# version Not much difference from Java In many examples change is a matter of style and naming conventions, such as use of Main Instead of main And Console.WriteLine()

18 Types: Better than C++ or Java  Simple types  character, integer, floating-point types  Boolean type  Derived types  array, string: aggregates of simple types  Enum, struct (lightweight-object)  Everything derives from object!!!  Safety with initialization rule

19 The foreach Statement (Arrays only) for (i = 0; i < arrayName.Length; ++i) process arrayName[i]; foreach (T variableName in arrayName) process variableName; static void WriteArray(int[] a, String arrayName) { foreach (int element in a) Console.Write(element + " "); }

20 Iterator Logic: IEnumerable  foreach is important idea and safer construct than standard for iteration  Later, through use of interface inheritance, student can be taught to properly constructor their own collection class  Can use foreach

21 Call-By-Reference Improved Over Java  To modify variables passed as parameters, use call-by-reference  Designated by added keyword ref  ref used when declaring formal parameters when passing actual arguments  It is clear that ref variables are changed

22 Swap Program (1 of 2) class TestSwap { public static void Main() { int numOne = 1, numTwo = 2; Swap(ref numOne, ref numTwo); Console.WriteLine("numOne = " + numOne); Console.WriteLine("numTwo = " + numTwo); }

23 Swap Program (2 of 2) public static void Swap(ref int x, ref int y) { int temp; Console.WriteLine("x = " + x); Console.WriteLine("y = " + y); temp = x; x = y; y = temp; Console.WriteLine("x = " + x); Console.WriteLine("y = " + y); }

24 True Higher Dimensional Arrays  Jagged arrays as in C++ and Java  We have a[,,] arrays and appropriate methods and properties

25 TwoD Program class TwoD { public static void Main() { int[,] data = new int[3,5]; //row x column Console.WriteLine("No. of elements = " + data.Length); for (int i=0; i<data.GetLength(0); ++i) { Console.WriteLine("Row " + i + ": "); for (int j=0; j<data.GetLength(1); ++j) { data[i,j] = i * j; Console.Write(data[i,j] + ", "); } Console.WriteLine(); } } }

26 Improved Operator Overloading  Associated assignment op implicitly overloaded  Ordinary assignment cannot be overloaded  Overloaded as static methods  Unary operators take single argument of class type  Binary operators take one or both arguments of class type

27 System.Object  All types inherit form class System.Object including simple native types class Person { ····· public override string ToString() { return (name + ", age is " + age + ", gender is " + gender); }

28 Object and Un/ Boxing Example bool flag = true, answer; double x = 3.5; object any = flag; // boxing value to // reference type answer = (bool)flag // unboxing any = x; // boxing double value  When method defined for Object such as ToString( ) is called on value type, value is boxed  Works with value types as arguments to Write( ) and WriteLine( )

29 Property using get and set and value public class Point { public override string ToString() { return String.Format("({0}, {1})", x, y); } public double X // property for manipulating x { get { return x; } set { x = value; } } public double Y // property for manipulating y { get { return y; } set { y = value; } } private double x, y; }

30 Set and Get The C# language gives a standard rather than an ad hoc scheme for providing access and mutation to underlying state variables. This teaches methodology in a more straightforward way Note the style of using X (capitalized) for the hidden x (lowercase)

31 Java and C++ Comparisons  As in C#, Java and C++ have constructors  C++ does not have garbage collection and relies on destructors to deallocate memory using delete  In Java, classes include ToString( ), returns a string representation of class  In C#, we can implement ToString( )

32 Why C# Is Better Than Java  In C#, everything is an object  C# native types to be passed by reference  C# has boxing and unboxing for generic routines  C# foreach & IEnumerator for iterator  C# has true multi-dimensional arrays  C# get and set as access methods for retrieving and changing data members values  C# programmer can readily convert Java code to C#, but not vice versa

33 Why C# Is Better Than Java  C# get and set to access properties for retrieving and changing data members values  Keeps data hiding with consistent methodology  The C# programmer can readily convert Java code to C#, but not vice versa

34 Why C# Is Better Than Java  C# has enum as type and struct as lightweight object  C# programmer can use pointers but most label code unsafe  CLR and interoperable code – such as managed C++ and VB

35 Why C# Is Better Than C++  C++ too complex and unsafe  C++ system-dependent  C++ not Web-ready  C++ does not manage memory  C++ has pointer types

36 C# and Treatment of Type  Native simple types, such as int, bool, and double are value types  User-defined class types are reference type  string is a reference type  Reference types such as person call a constructor to build instances of types  Otherwise special value null : empty reference value

37 Final Thoughts  C# nicely improved on Java and C++  C# can be used for everything  sockets, threading, events, sql, …XML  C# by Dissection: a plug  Parallel treatment  Students probably use all 4 languages: C, C++, Java and C#


Download ppt "C#: CS1 Should We Switch ? WG2.4 AUGUST 7,2003 IRA POHL  ALGOL – PL/1 – Pascal – C / C++ / Java / C#  What have we learned from CS1 choices  Neat Features."

Similar presentations


Ads by Google