C#: CS1 Should We Switch ? WG2.4 AUGUST 7,2003 IRA POHL ALGOL – PL/1 – Pascal – C / C++ /...

37
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++ [email protected]
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    213
  • download

    0

Transcript of C#: CS1 Should We Switch ? WG2.4 AUGUST 7,2003 IRA POHL ALGOL – PL/1 – Pascal – C / C++ /...

Page 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.

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++ [email protected]

ira pohl
Talk given at USC Microsoft/AWL conference on april 20, 2002. Them what language to use to teach CS1 and why C# is a strong candidate.
Page 2: 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.

Family of Languages

ira pohl
Grandmother simula 67 needs to be labeled.Molly's cartoon needs some improvement.Idea how C,...C# inherited its theme and language design.
Page 3: 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.

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

ira pohl
How programming language and programming methodology affect cs-1
Page 4: 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.

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)

Page 5: 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.

Copyright Ira Pohl 2002 5

ALGOL 60

BLOCKS,TYPE DECLARATION,RECURSIONBNF, FOR, CALL BY NAME,PUBLICATION, PROCEDURAL ENCAPSULATION

CS IS DOMINATED BY NUMERICAL SCIENCE

CHIEF COMPETITION IS FORTRAN

Page 6: 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.

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;

Page 7: 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.

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

Page 8: 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.

Copyright Ira Pohl 2002 8

MODULE AND ADT1980

PASCAL AND STEPWISE REFINEMENT

WEAK LIBRARIES, NON-MODULAR

SO UCSD PASCAL, MODULA2, C, ADA

SHOULD WIRTH HAVE DEVELOPED PASCAL II

Page 9: 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.

Copyright Ira Pohl 2002 9

OO AND UNIX 1985

LARGER SCALE UNIX PROJECT INFLUENCESTUDENTS WANT JOB SKILLS MAKE AND FILES

SMALLTALK OO VISIBILITY INCREASES

C, C++, MOULA 2 AND PASCAL, ML

Page 10: 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.

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

Page 11: 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.

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#)

Page 12: 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.

Traditional First C# Program

// Hello world in C#// by Olivia Programmer

using System;class Hello {  public static void Main()  {    Console.WriteLine("Hello, world!");  }}

Page 13: 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.

“The Alpha Centarians say Hello in C#!”

Page 14: 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.

Make change in C++ // Change in dimes and pennies

#include <iostream>using namespace std;

int main(){  int  price, change, dimes, pennies;

  cout << "Enter price (0:100):";  cin >> 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;}

Page 15: 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.

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");  }}

Page 16: 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.

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.");  }}

Page 17: 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.

C# version

Not much difference from Java

In many examples change is a matter of style and naming conventions, such as use of MainInstead of main

And Console.WriteLine()

Page 18: 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.

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

Page 19: 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.

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 + "  ");}

Page 20: 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.

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

Page 21: 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.

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

Page 22: 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.

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); }

Page 23: 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.

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); }}

Page 24: 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.

True Higher Dimensional Arrays

Jagged arrays as in C++ and Java

We have a[,,] arrays and appropriate methods and properties

Page 25: 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.

TwoD Programclass 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(); } }}

Page 26: 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.

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

Page 27: 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.

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);  }

Page 28: 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.

Object and Un/ Boxing Example

bool flag = true, answer;double x = 3.5;object any = flag; // boxing value to // reference typeanswer = (bool)flag  // unboxingany = 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( )

Page 29: 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.

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;}

Page 30: 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.

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)

Page 31: 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.

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( )

Page 32: 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.

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

Page 33: 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.

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

Page 34: 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.

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

Page 35: 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.

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

Page 36: 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.

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

Page 37: 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.

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#