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# Signed? Bytes Possible
Type Occupied Values
sbyte Yes 1 -128 to 127
short Yes 2 -32768 to 32767
int Yes 4 -2147483648 to
2147483647
long Yes 8 -9223372036854775808 to
9223372036854775807
byte No 1 0 to 255
ushort No 2 0 to 65535
uint No 4 0 to 4294967295
ulong No 8 0 to
18446744073709551615
Built-in Types
C# Signed? Bytes Possible
Type Occupied Values
float Yes 4 Approximately ±1.5 x 10-45
to ±3.4 x 1038 with 7
significant figures
double Yes 8 Approximately ±5.0 x 10-
324
to ±1.7 x 10308 with 15
or 16 significant figures
decimal Yes 12 Approximately ±1.0 x 10-28
to ±7.9 x 1028 with 28 or 29
significant figures
char N/A 2 Any Unicode character (16
bit)
bool N/A 1/2 true 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(); after this statement
x.myValue = 10; both ‘x.myValue’ and
‘y.myValue’ equal 20
object y = x;
y.myValue = 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