SlideShare a Scribd company logo
1 of 24
Java vs. C#
What is C# ?
 What do you guys think?
 C# a new programming language or a new version
of C/C++ ?
 It is a strong language for network and internet programming. C#
has redefined the programming landscape. In addition, C# designed
with the need of C/C++ and Java programmers. This new language
has been developed specifically with the .NET framework in mind,
and as such is designated to be the .NET developer's language of
choice. One very important matter about C#, it is the first component
oriented programming language.
Differences with JAVA!
1. Subtle Differences in terms of syntax of
Java and C#
2. Slight modification of concepts in C# that
already exist in Java
3. Language features and concepts that do not
exist in Java at all.
1. Differences in terms of Syntax:
Java main  C# Main
Java:
public static void main(String[] args)
C#:
static void Main(string[] args)
 string is shorthand for the System.String class in C#. Another
interesting point is that in C#, your Main method can actually be
declared to be parameter-less
static void Main()
1. Differences in terms of Syntax:
Print statements
Java:
System.out.println("Hello world!");
C#:
System.Console.WriteLine("Hello world!");
or
Console.WriteLine("Hello again!");
1. Differences in terms of Syntax:
Declaring Constants
Java:
 In Java, compile-time constant values are declared inside a class as
static final int K = 100;
C#:
 To declare constants in C# the const keyword is used for compile time
constants while the readonly keyword is used for runtime constants. The
semantics of constant primitives and object references in C# is the same as
in Java.
const int K = 100;
1. Differences in terms of Syntax:
Inheritance
 C# uses C++ syntax for inheritance, both for class inheritance and interface
implementation as opposed to the extends and implements keywords.
Java:
class B extends A implements Comparable
{ ……………
……………
}
C#:
class B:A, IComparable
{ …………
…………
}
1. Differences in terms of Syntax:
Primitive Types
 In Java, all integer primitive types (byte, short, int, long) are signed by
default.
 In C# there are both signed and unsigned varieties of these types:
Unsigned Signed Size
byte sbyte 8 bits
ushort short 16 bits
uint int 32 bits
ulong long 64 bits
 The only significantly different primitive in C# is the decimal type, a type
which stores decimal numbers without rounding errors. Eg:
decimal dec = 100.44m;
1. Differences in terms of Syntax:
Array Declaration
 Java has two ways in which one can declare an array:
int[] iArray = new int[100]; //valid
float fArray[] = new float[100]; //valid
 C# uses only the latter array declaration syntax:
int[] iArray = new int[100]; //valid
float fArray[] = new float[100]; //ERROR: Won't compile
2. Modified concepts from Java:
Polymorphism & Overriding
 The means of implementing polymorphism typically involves having
methods in a base class that may be overridden by derived classes. These
methods can be invoked even though the client has a reference to a base
class type which points to an object of the derived class. Such methods are
bound at runtime instead of being bound during compilation and are typically
called virtual methods.
 In Java all methods are virtual methods while in C#, as in C++, one must
explicitly state which methods one wants to be virtual since by default they
are not.
 To mark a method as virtual in C#, one uses the virtual keyword. Also,
implementers of a child class can decide to either explicitly override the
virtual method by using the override keyword or explicitly choose not to
by using the new keyword instead
2. Modified concepts from Java:
Polymorphism & Overriding
Example:
using System;
public class Parent
{ public virtual void DoStuff(string str)
{ Console.WriteLine("In Parent.DoStuff: " + str);
}
}
public class Child: Parent
{ public void DoStuff(int n)
{ Console.WriteLine("In Child.DoStuff: " + n);
}
public override void DoStuff(string str)
{ Console.WriteLine("In Child.DoStuff: " + str);
}
}
public new void DoStuff(string str)
2. Modified concepts from Java:
Operator Overloading
 Operator overloading allows standard operators in a language to be
given new semantics when applied in the context of a particular class or
type.
 Operator overloading can be used to simplify the syntax of certain
operations especially when they are performed very often, such as
string concatenation in Java or interactions with iterators and collections
in the C++ Standard Template Library.
 Unlike C++, C# does not allow the overloading of the following
operators; new,( ), ||, &&, =, or any variations of compound
assignments such as +=, -=, etc.
2. Modified concepts from Java:
Switch Statements
 There are two major differences between the switch statement in C# versus
that in Java.
 In C#, switch statements support the use of string literals and do not allow
fall-through unless the label contains no statements.
 switch(foo){
case "A": Console.WriteLine("A seen");
break;
case "B":
case "C": Console.WriteLine("B or C seen");
break;
/* ERROR: Won't compile due to fall-through at case "D" */
case "D": Console.WriteLine("D seen");
case "E": Console.WriteLine("E seen");
break;
}
2. Modified concepts from Java:
Multiple Classes in a Single File
 Multiple classes can be defined in a single file in both languages
with some significant differences.
 In Java, there can only be one class per source file that has public
access and it must have the same name as the source file.
 C# does not have a restriction on the number of public classes that
can exist in a source file and neither is there a requirement for the
name of any of the classes in the file to match that of the source file.
2. Modified concepts from Java:
Importing Libraries
 Both the langugaes support this functionality and C#
follows Java’s technique for importing libraries:
 C#: using keyword
using System;
using System.IO;
using System.Reflection;
 Java: import keyword
import java.util.*;
import java.io.*;
3. New Concepts in C#:
Enumerations
 Java's lack of enumerated types leads to the use of
integers in situations that do not guarantee type safety.
 C# code:
public enum Direction {North=1, East=2, West=4, South=8};
Usage:
Direction wall = Direction.North;
 Java equivalent code will be:
public class Direction {
public final static int NORTH = 1;
public final static int EAST = 2;
public final static int WEST = 3;
public final static int SOUTH = 4;
}
Usage:
int wall = Direction.NORTH;
3. New Concepts in C#:
Enumerations
 Despite the fact the Java version seems to express more, it doesn't,
and is less type-safe, by allowing you to accidentally assign wall to
any int value without the compiler complaining.
 C# enumerations support the ToString method, so they can
report their value as string (such as “North") and not just an an
integer.
 There also exists a static Parse method within the Enum class for
converting a string to an enumeration.
3. New Concepts in C#:
foreach Statement
 The foreach loop is an iteration construct that is popular in a number
of scripting languages (e.g. Perl, PHP, Tcl/Tk)
 The foreach loop is a less verbose way to iterate through arrays or
classes that implement the the System.Collections.IEnumerable
interface.
 Example:
string[] greek_alphabet = {"alpha", "beta", "gamma", "delta"};
foreach(string str in greek_alphabet) {
Console.WriteLine(str + " is a greek letter");
}
3. New Concepts in C#:
Properties
 Properties are a way to abstract away from directly accessing the
members of a class, similar to how accessors (getters) and
modifiers (setters) are used in Java.
 Particularly for read/write properties, C# provides a cleaner way of
handling this concept. The relationship between a get and set
method is inherent in C#, while has to be maintained in Java.
 It is possible to create, read-only, write-only or read-write properties
depending on if the getter and setter are implemented or not.
3. New Concepts in C#:
Properties
 Java:
public int getSize()
{ return size;
}
public void setSize (int val)
{ size = val;
}
 C#:
public int Size
{ get {return size;
}
set {size = val;
}
}
3. New Concepts in C#:
Pointers
 Although core C# is like Java in that there is no access to a pointer
type that is analogous to pointer types in C and C++, it is possible to
have pointer types if the C# code is executing in an unsafe context.
 Pointer arithmetic can be performed in C# within methods marked
with the unsafe keyword.
 Example:
public static unsafe void Swap(int* a, int*b)
{ int temp = *a;
*a = *b;
*b = temp;
}
3. New Concepts in C#:
Pass by Refernce
 In Java the arguments to a method are passed by value meaning
that a method operates on copies of the items passed to it instead of
on the actual items.
 In C#, it is possible to specify that the arguments to a method
actually be references.
 In Java trying to return multiple values from a method is not
supported.

The C# keywords used are ref and out.
ChangeMe(out s);
Swap(ref a, ref b);
REFERENCES:
 OOP with Microsoft VB.NET and Microsoft Visual C#.NET
by Robin A. Reyonlds-Haerle
 JAVA 2 Essentials
by Cay Horstmann
Websites:
 Java vs. C#: Code to Code Comparison
http://www.javacamp.org/javavscsharp/
 A Comparative Overview of C#:
http://genamics.com/developer/csharp_comparative.htm
 C#: A language alternative or just J--?,
http://www.javaworld.com/javaworld/jw-11-2000/jw-1122-csharp1.html
 A COMPARISON OF C# TO JAVA By Dare Obasanjo
http://www.soften.ktu.lt/~mockus/gmcsharp/csharp/c-sharp-vs-java.html#foreach
 Conversational C# for Java Programmers by Raffi Krikorian
http://www.ondotnet.com/pub/a/dotnet/2001/05/31/csharp_4_java.html
Thank You…

More Related Content

What's hot

What's hot (20)

Wrapper class
Wrapper classWrapper class
Wrapper class
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
C# Exceptions Handling
C# Exceptions Handling C# Exceptions Handling
C# Exceptions Handling
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
C++
C++C++
C++
 
Introduction to .NET Core
Introduction to .NET CoreIntroduction to .NET Core
Introduction to .NET Core
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Angular routing
Angular routingAngular routing
Angular routing
 
C# operators
C# operatorsC# operators
C# operators
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 
Inheritance in OOPs with java
Inheritance in OOPs with javaInheritance in OOPs with java
Inheritance in OOPs with java
 
C# basics
 C# basics C# basics
C# basics
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Programming in c++ ppt
Programming in c++ pptProgramming in c++ ppt
Programming in c++ ppt
 
JavaFX Overview
JavaFX OverviewJavaFX Overview
JavaFX Overview
 
angular fundamentals.pdf
angular fundamentals.pdfangular fundamentals.pdf
angular fundamentals.pdf
 

Similar to Difference between Java and c#

C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
Zafor Iqbal
 
Migrating From Cpp To C Sharp
Migrating From Cpp To C SharpMigrating From Cpp To C Sharp
Migrating From Cpp To C Sharp
Ganesh Samarthyam
 

Similar to Difference between Java and c# (20)

C#unit4
C#unit4C#unit4
C#unit4
 
c# at f#
c# at f#c# at f#
c# at f#
 
C# AND F#
C# AND F#C# AND F#
C# AND F#
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
 
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
 
Csharp_mahesh
Csharp_maheshCsharp_mahesh
Csharp_mahesh
 
C-sharping.docx
C-sharping.docxC-sharping.docx
C-sharping.docx
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
 
C++ ppt
C++ pptC++ ppt
C++ ppt
 
C++ ppt.pptx
C++ ppt.pptxC++ ppt.pptx
C++ ppt.pptx
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
21UCAC61 C# and .Net Programming.pdf(MTNC)(BCA)
21UCAC61 C# and .Net Programming.pdf(MTNC)(BCA)21UCAC61 C# and .Net Programming.pdf(MTNC)(BCA)
21UCAC61 C# and .Net Programming.pdf(MTNC)(BCA)
 
Basic Structure Of C++
Basic Structure Of C++Basic Structure Of C++
Basic Structure Of C++
 
C sharp chap1
C sharp chap1C sharp chap1
C sharp chap1
 
C# note
C# noteC# note
C# note
 
Full CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java languageFull CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java language
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answer
 
Migrating From Cpp To C Sharp
Migrating From Cpp To C SharpMigrating From Cpp To C Sharp
Migrating From Cpp To C Sharp
 

Recently uploaded

Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 

Recently uploaded (20)

Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 

Difference between Java and c#

  • 2. What is C# ?  What do you guys think?  C# a new programming language or a new version of C/C++ ?  It is a strong language for network and internet programming. C# has redefined the programming landscape. In addition, C# designed with the need of C/C++ and Java programmers. This new language has been developed specifically with the .NET framework in mind, and as such is designated to be the .NET developer's language of choice. One very important matter about C#, it is the first component oriented programming language.
  • 3. Differences with JAVA! 1. Subtle Differences in terms of syntax of Java and C# 2. Slight modification of concepts in C# that already exist in Java 3. Language features and concepts that do not exist in Java at all.
  • 4. 1. Differences in terms of Syntax: Java main  C# Main Java: public static void main(String[] args) C#: static void Main(string[] args)  string is shorthand for the System.String class in C#. Another interesting point is that in C#, your Main method can actually be declared to be parameter-less static void Main()
  • 5. 1. Differences in terms of Syntax: Print statements Java: System.out.println("Hello world!"); C#: System.Console.WriteLine("Hello world!"); or Console.WriteLine("Hello again!");
  • 6. 1. Differences in terms of Syntax: Declaring Constants Java:  In Java, compile-time constant values are declared inside a class as static final int K = 100; C#:  To declare constants in C# the const keyword is used for compile time constants while the readonly keyword is used for runtime constants. The semantics of constant primitives and object references in C# is the same as in Java. const int K = 100;
  • 7. 1. Differences in terms of Syntax: Inheritance  C# uses C++ syntax for inheritance, both for class inheritance and interface implementation as opposed to the extends and implements keywords. Java: class B extends A implements Comparable { …………… …………… } C#: class B:A, IComparable { ………… ………… }
  • 8. 1. Differences in terms of Syntax: Primitive Types  In Java, all integer primitive types (byte, short, int, long) are signed by default.  In C# there are both signed and unsigned varieties of these types: Unsigned Signed Size byte sbyte 8 bits ushort short 16 bits uint int 32 bits ulong long 64 bits  The only significantly different primitive in C# is the decimal type, a type which stores decimal numbers without rounding errors. Eg: decimal dec = 100.44m;
  • 9. 1. Differences in terms of Syntax: Array Declaration  Java has two ways in which one can declare an array: int[] iArray = new int[100]; //valid float fArray[] = new float[100]; //valid  C# uses only the latter array declaration syntax: int[] iArray = new int[100]; //valid float fArray[] = new float[100]; //ERROR: Won't compile
  • 10. 2. Modified concepts from Java: Polymorphism & Overriding  The means of implementing polymorphism typically involves having methods in a base class that may be overridden by derived classes. These methods can be invoked even though the client has a reference to a base class type which points to an object of the derived class. Such methods are bound at runtime instead of being bound during compilation and are typically called virtual methods.  In Java all methods are virtual methods while in C#, as in C++, one must explicitly state which methods one wants to be virtual since by default they are not.  To mark a method as virtual in C#, one uses the virtual keyword. Also, implementers of a child class can decide to either explicitly override the virtual method by using the override keyword or explicitly choose not to by using the new keyword instead
  • 11. 2. Modified concepts from Java: Polymorphism & Overriding Example: using System; public class Parent { public virtual void DoStuff(string str) { Console.WriteLine("In Parent.DoStuff: " + str); } } public class Child: Parent { public void DoStuff(int n) { Console.WriteLine("In Child.DoStuff: " + n); } public override void DoStuff(string str) { Console.WriteLine("In Child.DoStuff: " + str); } } public new void DoStuff(string str)
  • 12. 2. Modified concepts from Java: Operator Overloading  Operator overloading allows standard operators in a language to be given new semantics when applied in the context of a particular class or type.  Operator overloading can be used to simplify the syntax of certain operations especially when they are performed very often, such as string concatenation in Java or interactions with iterators and collections in the C++ Standard Template Library.  Unlike C++, C# does not allow the overloading of the following operators; new,( ), ||, &&, =, or any variations of compound assignments such as +=, -=, etc.
  • 13. 2. Modified concepts from Java: Switch Statements  There are two major differences between the switch statement in C# versus that in Java.  In C#, switch statements support the use of string literals and do not allow fall-through unless the label contains no statements.  switch(foo){ case "A": Console.WriteLine("A seen"); break; case "B": case "C": Console.WriteLine("B or C seen"); break; /* ERROR: Won't compile due to fall-through at case "D" */ case "D": Console.WriteLine("D seen"); case "E": Console.WriteLine("E seen"); break; }
  • 14. 2. Modified concepts from Java: Multiple Classes in a Single File  Multiple classes can be defined in a single file in both languages with some significant differences.  In Java, there can only be one class per source file that has public access and it must have the same name as the source file.  C# does not have a restriction on the number of public classes that can exist in a source file and neither is there a requirement for the name of any of the classes in the file to match that of the source file.
  • 15. 2. Modified concepts from Java: Importing Libraries  Both the langugaes support this functionality and C# follows Java’s technique for importing libraries:  C#: using keyword using System; using System.IO; using System.Reflection;  Java: import keyword import java.util.*; import java.io.*;
  • 16. 3. New Concepts in C#: Enumerations  Java's lack of enumerated types leads to the use of integers in situations that do not guarantee type safety.  C# code: public enum Direction {North=1, East=2, West=4, South=8}; Usage: Direction wall = Direction.North;  Java equivalent code will be: public class Direction { public final static int NORTH = 1; public final static int EAST = 2; public final static int WEST = 3; public final static int SOUTH = 4; } Usage: int wall = Direction.NORTH;
  • 17. 3. New Concepts in C#: Enumerations  Despite the fact the Java version seems to express more, it doesn't, and is less type-safe, by allowing you to accidentally assign wall to any int value without the compiler complaining.  C# enumerations support the ToString method, so they can report their value as string (such as “North") and not just an an integer.  There also exists a static Parse method within the Enum class for converting a string to an enumeration.
  • 18. 3. New Concepts in C#: foreach Statement  The foreach loop is an iteration construct that is popular in a number of scripting languages (e.g. Perl, PHP, Tcl/Tk)  The foreach loop is a less verbose way to iterate through arrays or classes that implement the the System.Collections.IEnumerable interface.  Example: string[] greek_alphabet = {"alpha", "beta", "gamma", "delta"}; foreach(string str in greek_alphabet) { Console.WriteLine(str + " is a greek letter"); }
  • 19. 3. New Concepts in C#: Properties  Properties are a way to abstract away from directly accessing the members of a class, similar to how accessors (getters) and modifiers (setters) are used in Java.  Particularly for read/write properties, C# provides a cleaner way of handling this concept. The relationship between a get and set method is inherent in C#, while has to be maintained in Java.  It is possible to create, read-only, write-only or read-write properties depending on if the getter and setter are implemented or not.
  • 20. 3. New Concepts in C#: Properties  Java: public int getSize() { return size; } public void setSize (int val) { size = val; }  C#: public int Size { get {return size; } set {size = val; } }
  • 21. 3. New Concepts in C#: Pointers  Although core C# is like Java in that there is no access to a pointer type that is analogous to pointer types in C and C++, it is possible to have pointer types if the C# code is executing in an unsafe context.  Pointer arithmetic can be performed in C# within methods marked with the unsafe keyword.  Example: public static unsafe void Swap(int* a, int*b) { int temp = *a; *a = *b; *b = temp; }
  • 22. 3. New Concepts in C#: Pass by Refernce  In Java the arguments to a method are passed by value meaning that a method operates on copies of the items passed to it instead of on the actual items.  In C#, it is possible to specify that the arguments to a method actually be references.  In Java trying to return multiple values from a method is not supported.  The C# keywords used are ref and out. ChangeMe(out s); Swap(ref a, ref b);
  • 23. REFERENCES:  OOP with Microsoft VB.NET and Microsoft Visual C#.NET by Robin A. Reyonlds-Haerle  JAVA 2 Essentials by Cay Horstmann Websites:  Java vs. C#: Code to Code Comparison http://www.javacamp.org/javavscsharp/  A Comparative Overview of C#: http://genamics.com/developer/csharp_comparative.htm  C#: A language alternative or just J--?, http://www.javaworld.com/javaworld/jw-11-2000/jw-1122-csharp1.html  A COMPARISON OF C# TO JAVA By Dare Obasanjo http://www.soften.ktu.lt/~mockus/gmcsharp/csharp/c-sharp-vs-java.html#foreach  Conversational C# for Java Programmers by Raffi Krikorian http://www.ondotnet.com/pub/a/dotnet/2001/05/31/csharp_4_java.html