Programming in C# for
C++/Java Programmers
JL Popyack,
Here are some of the major points about programming in C#, in contrast to C++/Java:
|
Feature |
C# |
C++ |
|
Character Set |
Unicode case-sensitive |
ASCII case-sensitive |
|
Operators/ |
Basically identical to C++: |
|
|
Type casting |
Has is, as operators for
class comparison, casting: x = variable as type |
x = (type)variable ; |
|
Class Usage |
Uses x.y notation. |
|
|
Class Definition |
No semicolon at end of class definition. this: current instance of an object |
Semicolon at end of class definition. this: current instance of an object |
|
Class Definition: set and get methods for
properties |
public class myClass { private int prop_ ; public int Prop { get //get method (inspector) { return prop_ ; } set //set method (mutator) { prop_ = value ; } } value is a built-in keyword. |
class myClass { public: … void setProp(int x) ; int getProp () const ; private: int prop_ ; }; void myClass::setProp(int
x) { prop_ = x ; } int myClass::getProp()
const { return prop_ ; } |
|
Console I/O |
Console.WriteLine("a={0},b={1}",a,b); |
printf("a=%i,b=%i",a,b); |
|
Strings |
immutable:
methods do not modify string methods:
numerous, including verbatim strings with @ operator – escape characters and line breaks kept verbatim: string vs = @"Escape
is \n, Tab is \t"; |
#include <string> string s = "Shiver me
timbers!"; |
|
Arrays ArrayLists |
Arrays have fixed
size;
type[] name; array methods:
numerous, including array properties: include Length, Rank Array Lists: ArrayList name = new
ArrayList(); ArrayList methods:
numerous, including ArrayList properties: include Capacity, Count |
#include
<vector> |
|
Iteration |
while, for, and do..while loops foreach
(type id in expr) ArrayList name = new ArrayList(); foreach(string s in name) s = "Whatever!" ; |
vector<string>
name(10); for(int i=0; i<name.size();
i++) name[i] = "Whatever!" ; |
|
Function Definition |
|
|
|
Argument Passing |
pass by reference:
ref e.g., public void getInt(ref int
x) call with t.getInt(ref x) variable number of arguments: params public int sum(params
int[] vals) { int temp=0 ; foreach(int n in vals) temp += n ; return temp ; } call with … |
void getInt(int &x) call with getInt(x) |
|
Random Number Generation |
// arbitrary starting
place Random RNG = new Random()
; // specific seed (of type
int) // Random RNG = new
Random(seed) ; for(int i=0; i<n; i++) { // Generate random # in [a,b) int k = RNG.Next(a,b) ; Console.WriteLine("k={0}",k) ; } |
#include <cstdlib> #include <ctime> // arbitrary starting
place unsigned int seed =
static_cast<unsigned int>( time(0)); // specific seed (of type
int) srand( seed ); // Generate random # in
[a,b) int temp = ( (b-a)*rand()
+ a ); |
|
Program Form |
|
|
|
Program Form |
using System; namespace myApplication { using System; public class myClass { static void { myClass c = new myClass(); int m = c.myFunction(3) ; Console.Write("m={0} ",m) ; } public int myFunction(int arg) { return arg+1 ; } } } |
// preprocessor #include <iostream> #include
"mystuff.h" using namespace std ; // prototypes int myFunction(int arg) ; // main program int main(void) { int m = myFunction(3) ; cout << "m=" << m ; return 0 ; } // function definitions int myFunction(int arg) { return arg+1 ; } |
|
|
using System; namespace myApplication { using System; public class myClass { private int m ; static void { myClass c = new myClass(); c.m = c.myFunction(3) ; Console.Write("m={0}",c.m) ; } public int myFunction(int arg) { return arg+1 ; } } } |
|