You are on page 1of 2

C # for Java Developers

Keywords Operators Common Datatypes Development Areas


Java C# Java C# Java C# Description Java C# Java Range C# Range
abstract abstract native extern x.y x.y Member access “dot” operator boolean bool true of false true/false
f(x) f(x) Method invocation operator byte sbyte
assert Debug.Assert new new -128 to 127 -128 to 127
(method) a[x] a[x] Array element access operator char char 0 to 216 - 1 0 to 216 - 1
break break null null ++, -- ++, -- Increment and decrement operators (pre and post- double double ±5.0 x 10- 324 to ±1.7 x 10308 ±5.0 x 10-324 to ±1.7 x 10308
case case package namespace fix) float float ±1.5 x 1045 to ±3.4 x 1038 ±1.5 x 1045 to ±3.4 x 1038
catch catch private private new new Object instantiation operator int int 231 to 231-1 -231 to 231-1
class class protected internal
instanceof is Type verification operator long long
const const public public -263 to 263-1 -263 to 263-1
(T)x (T)x Explicit cast operator short short
continue continue return return -215 to 215-1 -2 to 215-1
15

+, - +, - Addition and subtraction operators (binary). String string


default default static static n/a n/a
do do strictfp n/a Positive and negative operators (unary) Object object n/a n/a
else else super base + + String concatenation operator Date DateTime 1st Jan 1970 to 1st Jan 0001 to 31st Dec
enum enum switch switch ! ! Logical negation operator implementation dependent 9999
extends : synchronized lock &&, || &&, || Conditional AND and OR operators (short-circuited value
false false this this
evaluation)
final sealed throw throw
&, |, ^ n/a Conditional AND, OR, and XOR operators (full
finally finally throws n/a
for for / foreach transient [Nonserialized] evaluation of operands) Datatype examples
(attribute) ~ ~ Bitwise complement operator
goto goto true true
Java C#
&, |, ^ &, |, ^ Bitwise AND, OR, and XOR operators
if if try try <<, >> n/a Signed left-shift and right-shift operators int i = 1; int i = 1;
implements : … (varargs) params
>>> >> Unsigned right-shift operator byte b = 1; byte b = 1;
import using void void
instanceof is volatile volatile *, /, % *, /, % Multiply, divide, and modulus operators double d = 1.1; double d = 1.1;
interface interface while while ==, != ==, != Is-equal-to and is-not-equal-to operators long l = 1; long l = 1;
<, >, <=, <, >, <=, short s = 1; short s = 1;
Relational less-than, greater-than, less-than-or-equal-
Note: The const and goto keywords in Java have no function. >= >=
The C# const and goto keywords are operational. x?y:z x?y:z
to, and greater-than-or-equal-to operators
Conditional operator
boolean found = true; bool found = true; C# Features Not Available in Java 6
char c = 'z'; char c = 'z';
= = Assignment operator String title = "Hello World"; string title = "Hello World";
LINQ
C# keywords not found in Java, or with different behavior C# operators not available in Java: Delegates

Keyword Description Keyword Description Array examples Events


Operator Description Operator Description
base Provides access to operator Declares an overloaded Properties
<< Unsigned left-shift default(T) Returns the default value Java C#
members in a parent operator
class
operator for a type (generics) int[] data1; int[] data1; Indexers
<<= Unsigned left-shift sizeof(T) Returns the size of a int data2[]; n/a
checked Enables arithmetic out Declares an output
compound assignment type
Lambda Expressions
overflow checking parameter (method call)
Declares a covariant type
operator Partial Classes
?? Null-coalescing operator stackalloc Allocates a block of
parameter(generic interface)
memory on the stack
C# datatypes not provided as part of Java Dynamic Runtime Language
delegate Declares a type-safe override Declares a method that
=> Lambda expression typeof(e) Returns the type of an
reference to a method overrides a method in a base Type Range Size(bits) Structures
operator expression
class
event protected Declares a member that is
as Safe casting operator unchecked Disables arithmetic decimal 28-29 significant figures 128 Operator Overloading
Declares an event
overflow checking byte 0 to 255 8
accessible only within the
checked Enables arithmetic unsafe Enables unsafe code uint 232-1 32
Partial Methods
class and descendant classes
(different semantics from Java
overflow checking ulong 0 to 264 -1 64 Optional Parameters
ushort 0 to 2 -1
16
16
protected keyword)
explicit Declares a narrowing readonly Declares a field to be read-
conversion operator only
Java wrapper classes mapped to C# structs
implicit Declares a widening ref Declares a reference to a
conversion operator value type
C# identifiers that should not be used as names Java Wrapper C# Struct
in Declares a struct Defines a new value type
Boolean Boolean
contravariant type for types, methods, or variables:
Byte Byte
parameter for a
Character Char
generic interface dynamic join set
from let value n/a Decimal
new Declares a method virtual Declares a member that can
get orderby var Double Double
that hides a method in be overridden group partial where Float Float
a base class (method into select yield Integer Int32
modifier)
Long Int64
Short Int16
Void n/a

©2011 Microsoft Corporation. All Rights Reserved.


C # for Java Developers
Program and Class Example C# Java C# Struct and Enum Example

// Bring System namespace into scope package Example; enum Month


// - contains the Console class {
using System; // Optional import January, February, March, April, May, June,
import java.lang.*; July, August, September, October, November, December
namespace Example }
{ // Program class must be in a file called Program.java
// Program does not have to be in Program.cs // The main method must be defined in a public class struct Date
// Main method does not have to be in a public class {
class Program public class Program { private int year;
{ private Month month;
// Main method has a capital “M” public static void main(String[] args){ private int day;

static void Main(string[] args) Car myCar = new Car(); myCar.Drive(); public Date(int ccyy, Month mm, int dd)
{ } {
Car myCar = new Car(); } this.year = ccyy - 1900;
myCar.Drive(); this.month = mm;
} // Abstract base class this.day = dd - 1;
} abstract class Vehicle { }

// Abstract base class public void Drive() { public override string ToString()
abstract class Vehicle // Default implementation {
{ } string data = String.Format("{0} {1} {2}",
public virtual void Drive() } this.month, this.day + 1 ,this.year + 1900);
{ return data;
// Default implementation // Car inherits from Vehicle }
} class Car extends Vehicle {
} public void AdvanceMonth()
// Override default implementation {
// Car inherits from Vehicle public void Drive() { this.month++;
class Car : Vehicle if (this.month == Month.December + 1)
{ System.out.println("Car running"); {
// Override default implementation } this.month = Month.January;
public override void Drive() } this.year++;
{ }
Console.WriteLine("Car running"); }
} }
}
}

C# Switch Example
Interface Example C# Java
string month;

switch (month) // string variables supported


interface IDateCalculator interface IDateCalculator { {
{ double DaysBetween(Date from,Date to); case "Jan":
double DaysBetween(DateTime from,DateTime to); double HoursBetween(Date from,Date to); monthNum = 1;
double HoursBetween(DateTime from,DateTime to); } break; // doesn’t allow fall through
} case "Feb":
class DateCalculator implements IDateCalculator monthNum = 2;
class DateCalculator : IDateCalculator { break;
{ public double DaysBetween(Date start,Date end) { default:
public double DaysBetween(DateTime start,DateTime end) long startTimeMs = start.getTime(); Console.WriteLine("Invalid Value");
{ long endTimeMs = end.getTime(); break;
double diffInDays = long diffInMs = endTimeMs - startTimeMs; }
end.Subtract(start).TotalDays; double diffInDays =
return diffInDays; diffInMs / (24 * 60 * 60 * 1000);
} return diffInDays;
}
public double HoursBetween(DateTime start,DateTime end)
{ public double HoursBetween(Date start,Date end) {
double diffInHours = long startTimeMs = start.getTime();
end.Subtract(start).TotalHours;
return diffInHours;
long endTimeMs = end.getTime();
long diffInMs = endTimeMs - startTimeMs; C# Delegate/Lambda Example
} double diffInHours =
} diffInMs / (60 * 60 * 1000);
return diffInHours;
}
} 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; };

C# Java
int answer = add(x, y);

Console.WriteLine(answer); // output: 5
Properties Example }
}

class Circle public class Circle {


{
private double radiusCM; Properties property;
public double RadiusMeters double radiusCM;
{
get { return radiusCM / 100; } public Circle()
C# LINQ Example
set { radiusCM = value * 100; } {
} property = new Properties();
} }

class Program void setRadiusMeters(int rad) using System;


{ { using System.Linq;
static void Main(string[] args) radiusCM = rad /(double)100;
{ property.setProperty("RadiusMeters", class Program
Circle myCircle = new Circle(); Double.toString(radiusCM*100)); {
} static void Main(string[] args)
myCircle.RadiusMeters = 50; } {
double radius = myCircle.RadiusMeters; string[] names = { "John", "Jim", "Bert",
} public class Program {
"Harry" };
}
public static void main(String[]args){ var matchingNames = from n in names
Circle circ = new Circle();
where n.StartsWith("J") select n;
circ.setRadiusMeters(50); foreach (string match in matchingNames)
double radius = Console.WriteLine(match);
new Double((String) }
circ.property.get("RadiusMeters")); }
}
}

©2011 Microsoft Corporation. All Rights Reserved.

You might also like