C#	
  for	
  Java	
  Developers	
  

            Jussi	
  Pohjolainen	
  
Tampere	
  University	
  of	
  Applied	
  Sciences	
  
Overview	
  
•  Ecma	
  and	
  ISO	
  Standard	
  
•  Developed	
  by	
  MicrosoD,	
  uses	
  in	
  .NET	
  and	
  
   WP7	
  
•  Most	
  recent	
  version	
  is	
  C#	
  4.0	
  
Versions	
  
Common	
  Language	
  Infrastructure	
  (CLI)	
  
CLI	
  
•  CLI	
  is	
  an	
  open	
  specificaRon	
  that	
  describes	
  
   executable	
  code	
  and	
  runRme	
  environment	
  
•  CLI	
  is	
  core	
  of	
  
    –  MicrosoD	
  .NET	
  Framework	
  
    –  Mono	
  (Open	
  Source)	
  
    –  Portable.net	
  (Open	
  Source)	
  	
  
CTS,	
  CLS,	
  VES,	
  CIL	
  
•  Common	
  Type	
  System	
  (CTS)	
  
    –  A	
  set	
  of	
  data	
  types	
  and	
  operaRons	
  that	
  are	
  share	
  by	
  all	
  
       CTS-­‐compliant	
  programming	
  languages,	
  such	
  as	
  C#	
  and	
  VB	
  
•  Common	
  Language	
  SpecificaRon	
  (CLS)	
  
    –  Set	
  of	
  base	
  rules	
  to	
  which	
  any	
  language	
  targeRng	
  the	
  CLI	
  
       should	
  conform.	
  	
  
•  Virtual	
  ExecuRon	
  System	
  (VES)	
  
    –  VES	
  loads	
  and	
  executes	
  CLI-­‐compaRble	
  programs	
  
•  Common	
  Intermediate	
  Language	
  (CIL)	
  
    –  Intermediate	
  language	
  that	
  is	
  abstracted	
  from	
  the	
  
       plaXorm	
  hardware	
  (In	
  Java:	
  class)	
  
Mono:OSX	
  »	
  C#	
  File	
  
Mono:OSX	
  »	
  Building	
  
Mono:OSX	
  »	
  Running	
  
.NET	
  on	
  Windows	
  7	
  




    Add	
  C:WindowsMicrosoft.NETFrameworkv3.5 to	
  path!	
  
Common	
  Language	
  RunRme:	
  Mac	
  

                                  Dropbox	
  –	
  
                                    folder	
  
Common	
  Language	
  RunRme:	
  Win	
  




                                 And	
  run	
  
                                the	
  .exe	
  in	
  
                                Windows!	
  
Almost	
  the	
  Same	
  but	
  Not	
  Quite	
  

C#	
  
Keywords	
  
•  Single	
  rooted	
  class	
  hierarchy:	
  all	
  objects	
  
   inheritate	
  System.Object	
  
•  Almost	
  every	
  keyword	
  in	
  Java	
  can	
  be	
  found	
  
   from	
  C#	
  
    –  super                            ->   base
    –  instanceof                       ->   is
    –  import                           ->   using
    –  extends / implements             ->   :
•  Otherwise,	
  pre`y	
  much	
  the	
  same	
  
Memory	
  Handling	
  and	
  RunRme	
  
•  Memory	
  Handling	
  
   –  Most	
  objects	
  in	
  C#	
  to	
  heap	
  using	
  new	
  
   –  CLR	
  handles	
  garbage	
  collecRons	
  
•  RunRme	
  
   –  C#	
  is	
  compiled	
  to	
  intermediate	
  langage	
  (IL)	
  
   –  IL	
  runs	
  on	
  top	
  of	
  CLR	
  
   –  IL	
  code	
  is	
  always	
  naRvely	
  compiled	
  before	
  running	
  
OO	
  
•  No	
  global	
  methods,	
  just	
  like	
  in	
  Java	
  
•  Interface	
  is	
  pure	
  abstract	
  class	
  
•  No	
  mulRple	
  inheritance	
  
	
  
Main	
  
 using System;

 class A {

    public static void Main(String[] args){

        Console.WriteLine("Hello World");

    }
}
Compiling	
  Several	
  Files	
  in	
  C#	
  
C:CodeSample> csc /main:A /out:example.exe A.cs B.cs

C:CodeSample> example.exe
 Hello World from class A

C:CodeSample> csc /main:B /out:example.exe A.cs B.cs

C:CodeSample> example.exe
 Hello World from class B
Inheritance:	
  Base	
  Class	
  
using System;
abstract public class Figure {
    private int x;
    private int y;
    public Figure(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public void SetX(int x) {
        this.x = x;
    }
    public void SetY(int y) {
        this.y = y;
    }
    public int GetY() {
        return y;
    }
    public int GetX() {
        return x;
    }
    public abstract double CalculateSurfaceArea();
}
Inheritance:	
  Interface	
  
interface IMove {
    void MoveTo(int x, int y);
}
Inheritance	
  
public class Rectangle : Figure, IMove {
    private int width;
    private int height;

    public Rectangle(int x, int y, int width, int height) : base(x,y) {
        this.width = width;
        this.height = height;
    }

    public void MoveTo(int x, int y) {
        SetX( GetX() + x );
        SetY( GetY() + y );
    }

    public override double CalculateSurfaceArea() {
        return width * height;
    }

    public static void Main(String [] args) {
        Rectangle r = new Rectangle(4, 5, 10, 10);
        Console.Write( "Rectangle x: " );
        Console.Write( r.GetX() );
        r.MoveTo(5, 0);
        Console.Write( "nRectangle y: " );
        Console.Write( r.GetX() );
        Console.Write( "nRectangle Surface Area: " );
        Console.Write( r.CalculateSurfaceArea() );
        Console.Write( "n" );
    }
}
Run	
  Time	
  Type	
  IdenRficaRon	
  
public static void Main(String [] args) {
       Object rectangleObject = new Rectangle(4, 5, 10, 10);

       // Type cast from Object to Rectangle
       Rectangle temp1 = rectangleObject as Rectangle;

       // Check if cast was successfull
       if(temp1 != null) {
           Console.Write("Success: Object was casted to Rectangle!");
           temp1.SetX(0);
       }
   }
ProperRes	
  
public class Rectangle : Figure, IMove {
    private int height;
    private int width;

    public int Width {
        get {
            return width;
        }
        set {
            if(value > 0) {
                width = value;
            } else {
                Console.WriteLine("Value was not set.");
            }
        }
    }

    public Rectangle(int x, int y, int width, int height) : base(x,y) {
        this.width = width;
        this.height = height;
    }

    …

    public static void Main(String [] args) {
        Rectangle r = new Rectangle(5,5,10,10);
        r.Width = 10;
        Console.Write(r.Width);
        // Value was not set
        r.Width = -5;
    }
}
Alias	
  
using Terminal = System.Console;

class Test {
     public static void Main(string[] args){
       Terminal.WriteLine("Please don’t use this");
     }
}
Namespaces	
  
using System;

namespace fi.tamk.tiko.ohjelmistotuotanto {
    public class Test {
        public static void method() {
            Console.Write("ohjelmistotuotanton");
        }
    }
}

namespace fi.tamk.tiko.pelituotanto {
    public class Test {
        public static void method() {
            Console.Write("pelituotanton");
        }
    }
}

class App {
    public static void Main(String [] args) {
        fi.tamk.tiko.ohjelmistotuotanto.Test.method();
        fi.tamk.tiko.pelituotanto.Test.method();
    }
}
Constant	
  Variables	
  
•  const int VARIABLE = 10;
Variable	
  Length	
  Parameters	
  and	
  foreach	
  
using System;

public class Params {

    public static void Method(params int[] array) {
        foreach(int num in array) {
            Console.Write(num);
            Console.Write("n");
        }
    }

    public static void Main(String [] args) {
        Method(1,2,3,4,5);
        Method(1,2);

        int [] myArray = {1,2,3,4};
        Method(myArray);
    }
}
OperaRon	
  Overloading	
  
using System;

public class MyNumber {

    private int value;

    public MyNumber(int value) {
        this.value = value;
    }

    public static MyNumber operator+(MyNumber number1, MyNumber number2) {
        int sum = number1.value + number2.value;
        MyNumber temp = new MyNumber(sum);

        return temp;
    }

    public static void Main(String [] args) {
        MyNumber number1 = new MyNumber(5);
        MyNumber number2 = new MyNumber(5);

        MyNumber sum = number1 + number2;

        Console.Write(sum.value);
    }
}
ParRal	
  Types	
  
•  The	
  parRal	
  types	
  feature	
  enables	
  one	
  to	
  
   define	
  a	
  single	
  class	
  across	
  mul/ple	
  source	
  
   files!	
  
•  So	
  one	
  class	
  can	
  be	
  declared	
  in	
  several	
  files!	
  
C# for Java Developers
C# for Java Developers
C# for Java Developers
C# for Java Developers
C# for Java Developers