35
C# for Java Developers Jussi Pohjolainen Tampere University of Applied Sciences

C# for Java Developers

Embed Size (px)

Citation preview

Page 1: C# for Java Developers

C#  for  Java  Developers  

Jussi  Pohjolainen  Tampere  University  of  Applied  Sciences  

Page 2: C# for Java Developers

Overview  

•  Ecma  and  ISO  Standard  •  Developed  by  MicrosoD,  uses  in  .NET  and  WP7  

•  Most  recent  version  is  C#  4.0  

Page 3: C# for Java Developers

Versions  

Page 4: C# for Java Developers

Common  Language  Infrastructure  (CLI)  

Page 5: C# for Java Developers

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)    

Page 6: C# for Java Developers

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)  

Page 7: C# for Java Developers

Mono:OSX  »  C#  File  

Page 8: C# for Java Developers

Mono:OSX  »  Building  

Page 9: C# for Java Developers

Mono:OSX  »  Running  

Page 10: C# for Java Developers

.NET  on  Windows  7  

Add  C:\Windows\Microsoft.NET\Framework\v3.5 to  path!  

Page 11: C# for Java Developers

Common  Language  RunRme:  Mac  

Dropbox  –  folder  

Page 12: C# for Java Developers

Common  Language  RunRme:  Win  

And  run  the  .exe  in  Windows!  

Page 13: C# for Java Developers

C#  Almost  the  Same  but  Not  Quite  

Page 14: C# for Java Developers

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  

Page 15: C# for Java Developers

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  

Page 16: C# for Java Developers

OO  

•  No  global  methods,  just  like  in  Java  •  Interface  is  pure  abstract  class  •  No  mulRple  inheritance    

Page 17: C# for Java Developers

Main   using System; class A { public static void Main(String[] args){ Console.WriteLine("Hello World"); } }

Page 18: C# for Java Developers

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

Page 19: C# for Java Developers

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

Page 20: C# for Java Developers

Inheritance:  Interface  

interface IMove {

void MoveTo(int x, int y);

}

Page 21: C# for Java Developers

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

Page 22: C# for Java Developers
Page 23: C# for Java Developers

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

}

}

Page 24: C# for Java Developers

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

Page 25: C# for Java Developers

Alias  using Terminal = System.Console;

class Test {

public static void Main(string[] args){

Terminal.WriteLine("Please don’t use this");

}

}

Page 26: C# for Java Developers

Namespaces  using System; namespace fi.tamk.tiko.ohjelmistotuotanto { public class Test { public static void method() { Console.Write("ohjelmistotuotanto\n"); } } } namespace fi.tamk.tiko.pelituotanto { public class Test { public static void method() { Console.Write("pelituotanto\n"); } } } class App { public static void Main(String [] args) { fi.tamk.tiko.ohjelmistotuotanto.Test.method(); fi.tamk.tiko.pelituotanto.Test.method(); } }

Page 27: C# for Java Developers

Constant  Variables  

•  const int VARIABLE = 10;

Page 28: C# for Java Developers

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

Page 29: C# for Java Developers

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

Page 30: C# for Java Developers

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!  

Page 31: C# for Java Developers
Page 32: C# for Java Developers
Page 33: C# for Java Developers
Page 34: C# for Java Developers
Page 35: C# for Java Developers