c# for java developers
TRANSCRIPT
![Page 1: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/1.jpg)
C# for Java Developers
Jussi Pohjolainen Tampere University of Applied Sciences
![Page 2: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/3.jpg)
Versions
![Page 4: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/4.jpg)
Common Language Infrastructure (CLI)
![Page 5: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/7.jpg)
Mono:OSX » C# File
![Page 8: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/8.jpg)
Mono:OSX » Building
![Page 9: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/9.jpg)
Mono:OSX » Running
![Page 10: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/10.jpg)
.NET on Windows 7
Add C:\Windows\Microsoft.NET\Framework\v3.5 to path!
![Page 11: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/11.jpg)
Common Language RunRme: Mac
Dropbox – folder
![Page 12: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/12.jpg)
Common Language RunRme: Win
And run the .exe in Windows!
![Page 13: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/13.jpg)
C# Almost the Same but Not Quite
![Page 14: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/16.jpg)
OO
• No global methods, just like in Java • Interface is pure abstract class • No mulRple inheritance
![Page 17: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/17.jpg)
Main using System; class A { public static void Main(String[] args){ Console.WriteLine("Hello World"); } }
![Page 18: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/20.jpg)
Inheritance: Interface
interface IMove {
void MoveTo(int x, int y);
}
![Page 21: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/22.jpg)
![Page 23: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/26.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/27.jpg)
Constant Variables
• const int VARIABLE = 10;
![Page 28: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/28.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/29.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/30.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/31.jpg)
![Page 32: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/32.jpg)
![Page 33: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/33.jpg)
![Page 34: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/34.jpg)
![Page 35: C# for Java Developers](https://reader033.vdocuments.net/reader033/viewer/2022050808/554f7477b4c9052a518b463a/html5/thumbnails/35.jpg)