Uploaded by Biwash Shrestha

CSharp for Java Developers - Cheat Sheet

advertisement
#
C
for Java Developers
Keywords
Java
C#
Java
C#
abstract
abstract
native
extern
assert
Debug.Assert
(method)
break
case
catch
class
const
continue
new
new
null
package
private
protected
public
return
null
namespace
private
internal
public
return
break
case
catch
class
const
continue
default
do
else
enum
extends
false
final
finally
for
goto
if
implements
import
instanceof
interface
default
do
else
enum
:
false
sealed
finally
for / foreach
goto
if
:
using
is
interface
static
strictfp
super
switch
synchronized
this
throw
throws
transient
true
try
… (varargs)
void
volatile
while
static
n/a
base
switch
lock
this
throw
n/a
[Nonserialized]
(attribute)
true
try
params
void
volatile
while
Note: The const and goto keywords in Java have no function.
The C# const and goto keywords are operational.
Java
C#
Description
x.y
x.y
Java
Member access “dot” operator
boolean bool
f(x)
a[x]
++, --
C# keywords not found in Java, or with different behavior
Keyword
Description
Keyword
Description
base
Provides access to
operator
Declares an overloaded
members in a parent
Enables arithmetic
overflow checking
new
Declares an event
Increment and decrement operators (pre and post-
new
(T)x
+, -
float
Object instantiation operator
int
Type verification operator
(T)x
long
Explicit cast operator
+, -
Addition and subtraction operators (binary).
Positive and negative operators (unary)
+
!
&&, ||
+
String concatenation operator
!
Logical negation operator
&&, ||
Declares a widening
&, |, ^
n/a
Conditional AND, OR, and XOR operators (full
evaluation of operands)
~
&, |, ^
<<, >>
>>>
*, /, %
==, !=
<, >, <=,
>=
x?y:z
~
Bitwise complement operator
&, |, ^
Bitwise AND, OR, and XOR operators
n/a
Signed left-shift and right-shift operators
>>
Unsigned right-shift operator
*, /, %
Multiply, divide, and modulus operators
==, !=
Is-equal-to and is-not-equal-to operators
<, >, <=,
>=
Relational less-than, greater-than, less-than-or-equal-
x?y:z
Conditional operator
to, and greater-than-or-equal-to operators
=
Assignment operator
in
Declares a
that hides a method in
a base class (method
modifier)
231 to 231-1
-231 to 231-1
-263 to 263-1
-263 to 263-1
-215 to 215-1
-2 to 215-1
n/a
n/a
n/a
n/a
int
long
short
string
object
9999
value
Datatype examples
Java
C#
int i = 1;
byte b = 1;
double d = 1.1;
long l = 1;
short s = 1;
int i = 1;
byte b = 1;
double d = 1.1;
long l = 1;
short s = 1;
boolean found = true;
char c = 'z';
String title = "Hello World";
bool found = true;
char c = 'z';
string title = "Hello World";
operator
Unsigned left-shift
sizeof(T)
compound assignment
Null-coalescing operator
Returns the size of a
int data2[];
Properties
Indexers
n/a
Lambda Expressions
type
stackalloc
Allocates a block of
memory on the stack
=>
Lambda expression
typeof(e)
operator
as
checked
Returns the type of an
Partial Classes
Enables arithmetic
unchecked
unsafe
C# datatypes not provided as part of Java
Dynamic Runtime Language
Type
Range
Size(bits)
Structures
Disables arithmetic
decimal
28-29 significant figures
128
Operator Overloading
overflow checking
byte
0 to 255
8
Enables unsafe code
uint
232-1
32
ulong
0 to 264 -1
64
ushort
0 to 2 -1
16
expression
Safe casting operator
LINQ
Events
Array examples
operator
??
C# Features Not Available in Java 6
Delegates
Declares an output
Declares a method that
1st Jan 0001 to 31st Dec
implementation dependent
<<=
Unsigned left-shift
Development Areas
15
DateTime 1st Jan 1970 to
int[] data1;
overflow checking
16
Partial Methods
Optional Parameters
Declares a field to be read-
Java wrapper classes mapped to C# structs
Declares a reference to a
Defines a new value type
generic interface
Declares a method
±1.5 x 1045 to ±3.4 x 1038
int[] data1;
parameter for a
virtual
±1.5 x 1045 to ±3.4 x 1038
for a type (generics)
contravariant type
new
float
operator
value type
struct
0 to 216 - 1
±5.0 x 10-324 to ±1.7 x 10308
C# operators not available in Java:
only
conversion operator
double
0 to 216 - 1
±5.0 x 10- 324 to ±1.7 x 10308
Conditional AND and OR operators (short-circuited
protected keyword)
ref
char
evaluation)
(different semantics from Java
implicit
Date
-128 to 127
C#
protected Declares a member that is
conversion operator
Object
-128 to 127
Java
overrides a method in a base
readonly
String
sbyte
Returns the default value
class and descendant classes
Declares a narrowing
short
true/false
default(T)
accessible only within the
explicit
double
true of false
<<
class
event
++, --
char
C# Range
Description
parameter(generic interface)
Declares a type-safe
reference to a method
Array element access operator
instanceof is
parameter (method call)
override
a[x]
byte
Java Range
Operator
Declares a covariant type
delegate
Method invocation operator
C#
Operator Description
class
out
f(x)
fix)
=
checked
Common Datatypes
Operators
Declares a member that can
be overridden
C# identifiers that should not be used as names
for types, methods, or variables:
dynamic
from
get
group
into
join
let
orderby
partial
select
set
value
var
where
yield
Java Wrapper
C# Struct
Boolean
Boolean
Byte
Byte
Character
Char
n/a
Decimal
Double
Double
Float
Float
Integer
Int32
Long
Int64
Short
Int16
Void
n/a
©2011 Microsoft Corporation. All Rights Reserved.
#
C
for Java Developers
Program and Class Example
#
C
Java
package Example;
// Bring System namespace into scope
// - contains the Console class
using System;
// Optional import
import java.lang.*;
namespace Example
{
// Program does not have to be in Program.cs
// Main method does not have to be in a public class
class Program
{
// Main method has a capital “M”
}
// Program class must be in a file called Program.java
// The main method must be defined in a public class
public class Program {
public static void main(String[] args){
static void Main(string[] args)
{
Car myCar = new Car();
myCar.Drive();
}
C# Struct and Enum Example
enum Month
{
January, February, March, April, May, June,
July, August, September, October, November, December
}
struct Date
{
private int year;
private Month month;
private int day;
Car myCar = new Car(); myCar.Drive();
}
}
public Date(int ccyy, Month mm, int dd)
{
this.year = ccyy - 1900;
this.month = mm;
this.day = dd - 1;
}
// Abstract base class
abstract class Vehicle {
// Abstract base class
abstract class Vehicle
{
public virtual void Drive()
{
// Default implementation
}
}
public void Drive() {
// Default implementation
}
}
public override string ToString()
{
string data = String.Format("{0} {1} {2}",
this.month, this.day + 1 ,this.year + 1900);
return data;
}
// Car inherits from Vehicle
class Car extends Vehicle {
// Override default implementation
public void Drive() {
// Car inherits from Vehicle
class Car : Vehicle
{
// Override default implementation
public override void Drive()
{
Console.WriteLine("Car running");
}
}
}
}
System.out.println("Car running");
}
public void AdvanceMonth()
{
this.month++;
if (this.month == Month.December + 1)
{
this.month = Month.January;
this.year++;
}
}
}
Interface Example
#
C
Java
C# Switch Example
string month;
interface IDateCalculator
{
double DaysBetween(DateTime from,DateTime to);
double HoursBetween(DateTime from,DateTime to);
}
class DateCalculator : IDateCalculator
{
public double DaysBetween(DateTime start,DateTime end)
{
double diffInDays =
end.Subtract(start).TotalDays;
return diffInDays;
}
}
public double HoursBetween(DateTime start,DateTime end)
{
double diffInHours =
end.Subtract(start).TotalHours;
return diffInHours;
}
interface IDateCalculator {
double DaysBetween(Date from,Date to);
double HoursBetween(Date from,Date to);
}
class DateCalculator implements IDateCalculator
{
public double DaysBetween(Date start,Date end) {
long startTimeMs = start.getTime();
long endTimeMs = end.getTime();
long diffInMs = endTimeMs - startTimeMs;
double diffInDays =
diffInMs / (24 * 60 * 60 * 1000);
return diffInDays;
}
}
public double HoursBetween(Date start,Date end) {
long startTimeMs = start.getTime();
long endTimeMs = end.getTime();
long diffInMs = endTimeMs - startTimeMs;
double diffInHours =
diffInMs / (60 * 60 * 1000);
return diffInHours;
}
switch (month) // string variables supported
{
case "Jan":
monthNum = 1;
break; // doesn’t allow fall through
case "Feb":
monthNum = 2;
break;
default:
Console.WriteLine("Invalid Value");
break;
}
C# Delegate/Lambda Example
class Program
{
delegate int Calculation(int a, int b);
static void Main(string[] args)
{
int x = 2;
int y = 3;
Calculation add = (a, b) => { return a + b; };
Properties Example
class Circle
{
private double radiusCM;
public double RadiusMeters
{
get { return radiusCM / 100; }
set { radiusCM = value * 100; }
}
}
#
C
Java
}
}
}
}
Console.WriteLine(answer); // output: 5
public class Circle {
Properties property;
double radiusCM;
public Circle()
{
property = new Properties();
}
class Program
{
static void Main(string[] args)
{
Circle myCircle = new Circle();
myCircle.RadiusMeters = 50;
double radius = myCircle.RadiusMeters;
int answer = add(x, y);
}
void setRadiusMeters(int rad)
{
radiusCM = rad /(double)100;
property.setProperty("RadiusMeters",
Double.toString(radiusCM*100));
}
public class Program {
C# LINQ Example
using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
string[] names = { "John", "Jim", "Bert",
"Harry" };
public static void main(String[]args){
Circle circ = new Circle();
}
}
circ.setRadiusMeters(50);
double radius =
new Double((String)
circ.property.get("RadiusMeters"));
var matchingNames = from n in names
where n.StartsWith("J") select n;
}
}
foreach (string match in matchingNames)
Console.WriteLine(match);
©2011 Microsoft Corporation. All Rights Reserved.
Download