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]
Family of Languages
CS1 through the years

Academic computer science in the 60’s


Confusion at the end of the 60’s


ALGOL 60 vs. FORTRAN
Pl/1, ALGOL W, BASIC, ALGOL 68
Pascal consensus
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)
ALGOL 60
BLOCKS,TYPE DECLARATION,RECURSION
BNF, FOR, CALL BY NAME,
PUBLICATION, PROCEDURAL ENCAPSULATION
CS IS DOMINATED BY NUMERICAL SCIENCE
CHIEF COMPETITION IS FORTRAN
Copyright Ira Pohl 2002
5
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;
Copyright Ira Pohl 2002
6
OH PASCAL -1970
NEED
PL1
NON NUMERICAL TYPES
ALGOL W
ALGOL W
ALGOL68
V. PASCAL
RECORDS
PASCAL
V. ALGOL 68
CHEAP, WIDESPREAD, INCREMENTALLY BETTER,
BOOKS
Copyright Ira Pohl 2002
7
MODULE AND ADT
1980
PASCAL AND STEPWISE REFINEMENT
WEAK LIBRARIES, NON-MODULAR
SO
UCSD PASCAL, MODULA2, C, ADA
SHOULD WIRTH HAVE DEVELOPED PASCAL II
Copyright Ira Pohl 2002
8
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
Copyright Ira Pohl 2002
9
JAVA 1995-2000
C++
C
TOO
COMPLEX
BUT OO NOW DOMINATES
TO PRIMITIVE
SCHEME
EIFFEL
….
NICHE
JAVA WORE{WRITE 1 RUN EVERWHERE}
APPLETS WEB NO POINTERS PURE OO
NEW
CONSENSUS
Copyright Ira Pohl 2002
10
CS1 Requirements

Modern and supports dominant
methodology




Multi-platform, excellent implementation
books
Type safety, assertions, error messages


Pascal: structured programming
C#, C++, Java: OOP
syntax, runtime
In Wide Use (has yet to happen for C#)
Traditional First C# Program
// Hello world in C#
// by Olivia Programmer
using System;
class Hello {
public static void Main()
{
Console.WriteLine("Hello, world!");
}
}
“The Alpha Centarians say Hello in C#!”
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;
";
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");
C# by Dissection chapter 1 example
using System;
class MakeChange {
public static void Main()
{
int price, change, dimes, pennies;
string data;
}
}
// declarations
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.");
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()
Types: Better than C++ or Java

Simple types



Boolean type
Derived types




character, integer, floating-point types
array, string: aggregates of simple types
Enum, struct (lightweight-object)
Everything derives from object!!!
Safety with initialization rule
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 + " ");
}
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
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
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);
}
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);
}
}
True Higher Dimensional Arrays

Jagged arrays as in C++ and Java

We have a[,,] arrays and appropriate
methods and properties
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(); }
}
}
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
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);
}
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( )
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;
}
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)
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( )
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
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
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
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
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
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#
Descargar

C# - day 1 - Jack Baskin School of Engineering