Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection coupled with the elimination of pointers Powerful reflection capabilities No header files, all code is written inline Classes all descended from ‘object’ and must be allocated on the heap with ‘new’ keyword Thread support (similar to Java)
Java and C# - Some Commonalities Single inheritance Interfaces No global functions or constants, everything belongs to a class Arrays and strings with built-in bounds checking The "." operator is always used (no more ->, :: operators) ‘null’ and ‘boolean’/’bool’ are keywords All values must be initialized before use Only Boolean expressions are allowed in ‘if’ statements Try Block can have a finally clause
Both C# and Java compile initially to an intermediate language: C# to Microsoft Intermediate Language (MSIL), and Java to Java bytecode In each case the intermediate language can be run by interpretation, or just-in-time compilation, on an appropriate 'virtual machine' In C#, however, more support is given for the further compilation of the intermediate language code into native code
Some C# features More primitive data types than Java More extension to value types Supports safer enumeration types Support for ‘struct’ – light weight objects Operator overloading 'delegates' - type-safe method pointers used to implement event-handling Three types of arrays: –Single dimensional –Multi-dimensional rectangular –Multi-dimensional jagged
Restricted use of pointers The 'switch' statements has been changed to disallow 'fall-through' behavior Support for class 'properties'
C# Hello World using System;// System namespace public class HelloWorld { public static void Main()// the Main function starts // with capital M { Console.WriteLine("Hello World!”); }
C# Hello World Case sensitive Everything has to be inside a class Name of the class and the name of the file in which it is saved don’t need to match You are free to choose any extension for the file, but it is usual to use the extension '.cs' Supports both single line and multiple line comments
Variable Types (1): Reference Types and Value Types C# is a type-safe language Variables can hold either value types or reference types, or they can be pointers Where a variable ‘v’ contains a value type, it directly contains an object with some value Where a variable ‘v’ contains a reference type, what it directly contains is something which refers to an object
Built-in Types C# Type Signed?Bytes Occupied Possible Values sbyteYes1-128 to 127 shortYes to intYes to longYes to byteNo10 to 255 ushortNo20 to uintNo40 to ulongNo80 to
Built-in Types C# Type Signed?Bytes Occupied Possible Values floatYes4Approximately ±1.5 x to ±3.4 x with 7 significant figures doubleYes8Approximately ±5.0 x to ±1.7 x with 15 or 16 significant figures decimalYes12Approximately ±1.0 x to ±7.9 x with 28 or 29 significant figures charN/A2Any Unicode character (16 bit) boolN/A1 / 2true or false
Structs Structs are significantly different than C++ In C++ a struct is exactly like a class, except that the default inheritance and default access are public rather than private In C# structs are very different from classes Structs in C# are designed to encapsulate lightweight objects They are value types (not reference types), so they're passed by value They are sealed, which means they cannot be derived from or have any base class other than ‘System.Value’Type, which is derived from Object Structs cannot declare a default (parameterless) constructor Structs are more efficient than classes
Properties Formalize the concept of getter/setter methods In C# the relationship between a get and set method is inherent, while in Java or C++ it has to be maintained
Properties Java/C++ Example public int getSize() { return size; } public void setSize (int value) { size = value; } foo.setSize (getSize () + 1);
Properties C# Example public int Size { get {return size; } set {size = value; } } foo.size = foo.size + 1;
Reference Types The two pre-defined reference types are object and string ‘object’ is the ultimate base class of all other types New reference types can be defined using 'class', 'interface', and 'delegate' declarations Reference types actually hold the value of a memory address occupied by the object they reference Aliasing object x = new object(); x.myValue = 10; object y = x; y.myValue = 20; after this statement both ‘x.myValue’ and ‘y.myValue’ equal 20
Reference Types No aliasing in strings – these are immutable –The properties of these objects can't change themselves –So in order to change what a string variable references, a new string object must be created string s1 = "hello"; string s2 = s1; s1 = “world”; //s1 points to a new string // s2 keeps pointing to the old string