University of Oxford |
|
Computing Services |
|
Comparing .NET with Java |
|
Author: Barry Cornelius Date: 29th October 2002; first created: 12th January 2002 |
size | C# | Visual Basic.NET | |
System.Boolean | 8 | bool | Boolean |
System.Byte | 8 | byte | Byte |
System.Int16 | 16 | short | Short |
System.Int32 | 32 | int | Integer |
System.Int64 | 64 | long | Long |
System.Float | 32 | float | Single |
System.Double | 64 | double | Double |
System.Char | 16 | char | Char |
System.Decimal | 128 | decimal | Decimal |
namespace First { public class Point { private int iX; private int iY; public Point(int pX, int pY) { iX = pX; iY = pY; } public override string ToString() { return iX + ":" + iY; } } }
using System; namespace First { public class PointTest { public static void Main() { Point tPoint = new Point(100, 200); Console.WriteLine(tPoint); Point tAnotherPoint = tPoint; Console.WriteLine(tAnotherPoint); } } }
namespace First { public class NamedPoint : Point { private string iName; public NamedPoint(string pName, int pX, int pY) : base(pX, pY) { iName = pName; } public override string ToString() { return iName + "%" + base.ToString(); } } }
using System; namespace First { public class NamedPointTest { public static void Main() { NamedPoint tNamedPoint = new NamedPoint("first", 100, 200); Console.WriteLine(tNamedPoint); Point tPoint = tNamedPoint; Console.WriteLine(tPoint); } } }
Public Class Point Private iX As Integer Private iY As Integer Public Sub New(ByVal pX As Integer, ByVal pY As Integer) iX = pX iY = pY End Sub Public Overrides Function ToString() As String Return iX & ":" & iY End Function End Class
Module PointTest Sub Main() Dim tPoint As Point = New Point(100, 200) Console.WriteLine(tPoint) Dim tAnotherPoint As Point = tPoint Console.WriteLine(tAnotherPoint) End Sub End Module
Public Class NamedPoint Inherits Point Private iName As String Public Sub New(ByVal pName As String, ByVal pX As Integer, ByVal pY As Integer) MyBase.New(pX, pY) iName = pName End Sub Public Overrides Function ToString() As String Return iName & "%" & MyBase.ToString() End Function End Class
Module NamedPointTest Sub Main() Dim tNamedPoint As NamedPoint = New NamedPoint("first", 100, 200) Console.WriteLine(tNamedPoint) Dim tPoint As Point = tNamedPoint Console.WriteLine(tPoint) End Sub End Module
We have now provided 4 classes in both C# and VB.NET:
We can mix the languages, e.g., use C#'s NamedPointTest class with VB.NET's NamedPoint class and C#'s Point class.
We now look at some of the ways in which C# and Visual Basic.NET are different from Java.
Here is an example of a struct type coded in C#:
namespace First { public struct SPoint { private int iX; private int iY; public SPoint(int pX, int pY) { iX = pX; iY = pY; } public override string ToString() { return iX + ":" + iY; } } }
Here is a method that uses this struct type:
using System; namespace First { public class SPointTest { public static void Main() { SPoint tSPoint = new SPoint(100, 200); Console.WriteLine(tSPoint); SPoint tAnotherSPoint = tSPoint; Console.WriteLine(tAnotherSPoint); } } }
enum Days : int { Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }
ArrayList tArrayList = new ArrayList();
i = 27; tArrayList.Add(i);an object is automatically created.
Point tPoint = new Point(100, 200); tArrayList.Add(tPoint);
If instead we use a struct type, boxing takes place:
SPoint tSPoint = new SPoint(300, 400); tArrayList.Add(tSPoint);
int i = 27; Integer tInteger = new Integer(i); tArrayList.Add(tInteger);
This means the value of the object is copied into the variable:
int j = (int) tArrayList[0]; Point tGotPoint = (Point) tArrayList[1]; SPoint tGotSPoint = (SPoint) tArrayList[2];
The following kinds of parameters are available:
Java | C# | VB.NET |
value | value | ByVal |
ref | ByRef | |
out | ||
params |
private static void Swap(ref int x, ref int y) { int temp = x; x = y; y = temp; } ... i = 42; j = 27; Swap(ref i, ref j); // i has the value 27 // j has the value 42
switch (tCommand) { case "add": ... case "remove": ... }
tOp = -1; switch (tCommand) { case "add": tOp = 1; goto case "remove"; case "remove": ... }
public class Point { private int iX; private int iY; public Point(int pX, int pY) { iX = pX; iY = pY; } public int X { get { return iX; } set { iX = value; } } ... } ... Point tPoint = new Point(100, 200); int tX = tPoint.X; // uses get tPoint.X = 150; // uses set tPoint.X++; // uses get and set
delegate int Analyse(string s);
private static int StringLength(string pString) { return pString.Length; }
Analyse tAnalyse = new Analyse(StringLength);The variable tAnalyse now contains a pointer to the StringLength method.
private static void iProcess(Analyse pAnalyse) { string tString = Console.ReadLine(); int tInt = pAnalyse(tString); Console.WriteLine(tInt); }
int tInt = pAnalyse(tString);
iProcess(tAnalyse);
delegate void EventHandler(object sender, EventArgs e);
public event EventHandler Click;
private Button iAddButton = new Button();
iAddButton.Click += new EventHandler(iHandleClick);
protected void iHandleClick(object sender, EventArgs e) { ... }
FileStream tFileStream = File.Open("data", FileMode.Open);This can cause the FileNotFoundException exception.
private void iProcessFile() throws FileNotFoundException { ... }
BufferedReader tKeyboard = new BufferedReader(new InputStreamReader(System.in)); String tAgeString = tKeyboard.readLine(); int tAge = Integer.parseInt(tAgeString);
string tAgeString = Console.ReadLine(); int tAge = int.Parse(tAgeString);
Here is a program that reads in a temperature given in degrees Centigrade and outputs the corresponding value in degrees Fahrenheit:
using System; namespace ConsoleConvert { class Class1 { [STAThread] static void Main() { Console.Write("Centigrade value: "); string tCentigradeString = Console.ReadLine(); double tCentigrade = double.Parse(tCentigradeString); double tFahrenheit = 32 + tCentigrade*9/5; Console.WriteLine("Fahrenheit value: " + tFahrenheit); } } }
Form Button TextBox Label MainMenu MenuItem
using System; using System.Drawing; using System.Windows.Forms; namespace MyWindowsConvert { public class Form1 : Form { private TextBox textBox1; private Button button1; private Label label1; public Form1() { textBox1 = new TextBox(); textBox1.Location = new Point(64, 32); textBox1.Size = new Size(120, 20); Controls.Add(textBox1); button1 = new Button(); button1.Location = new Point(64, 64); button1.Size = new Size(120, 20); button1.Text = "Get Fahrenheit"; button1.Click += new EventHandler(button1_Click); Controls.Add(button1); label1 = new Label(); label1.Location = new Point(64, 104); label1.Size = new Size(120, 20); Controls.Add(label1); Text = "MyWindowsConvert"; } private void button1_Click(object sender, EventArgs e) { double tCentigrade = double.Parse(textBox1.Text); double tFahrenheit = 32 + tCentigrade*9/5; label1.Text = tFahrenheit.ToString(); } public static void Main() { Form1 tForm1 = new Form1(); Application.Run(tForm1); } } }
<%@ ... language="c#" Codebehind="WebForm1.aspx.cs" ... Inherits="WebFormConvert.WebForm1" %> <form id="Form1" ... > <asp:TextBox id="TextBox1" ... </asp:TextBox> <asp:Button id="Button1" ... Text="Get Fahrenheit" ... </asp:Button> <asp:Label id="Label1" ... </asp:Label> </form>
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace WebFormConvert { public class WebForm1 : Page { protected TextBox TextBox1; protected Button Button1; protected Label Label1; private void Page_Load(object sender, EventArgs e) { } override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } private void Button1_Click(object sender, EventArgs e) { double tCentigrade = double.Parse(TextBox1.Text); double tFahrenheit = 32 + tCentigrade*9/5; Label1.Text = tFahrenheit.ToString(); } } }
POST /barry.cornelius/moving/WebServerConvert/Service1.asmx HTTP/1.1 Host: localhost Content-Type: text/xml; charset=utf-8 Content-Length: XXXX SOAPAction: "http://www.dur.ac.uk/barry.cornelius/webservices/ToFahrenheit" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ToFahrenheit xmlns="http://www.dur.ac.uk/barry.cornelius/webservices/"> <pCentigrade>0</pCentigrade> </ToFahrenheit> </soap:Body> </soap:Envelope>
HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: YYYY <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ToFahrenheitResponse xmlns="http://www.dur.ac.uk/barry.cornelius/webservices/"> <ToFahrenheitResult>32</ToFahrenheitResult> </ToFahrenheitResponse> </soap:Body> </soap:Envelope>
[WebService] public class Service1 : System.Web.Services.WebService { ... [WebMethod] public double ToFahrenheit(double pCentigrade) { return 32 + pCentigrade*9/5; } }
public class Service1 : System.Web.Services.Protocols.SoapHttpClientProtocol { ... public double ToFahrenheit(double pCentigrade) { object[] results = this.Invoke("ToFahrenheit", new object[]{pCentigrade}); return (double) results[0]; } ... }
Proxy.Service1 tService1 = new Proxy.Service1(); double tFahrenheit = tService1.ToFahrenheit(tCentigrade);