SlideShare una empresa de Scribd logo
1 de 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…

Más contenido relacionado

La actualidad más candente

Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
Doncho Minkov
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 

La actualidad más candente (20)

Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
C# - Part 1
C# - Part 1C# - Part 1
C# - Part 1
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
C# programming language
C# programming languageC# programming language
C# programming language
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Java Fundamentals
Java FundamentalsJava Fundamentals
Java Fundamentals
 
Java I/O
Java I/OJava I/O
Java I/O
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
C# Exceptions Handling
C# Exceptions Handling C# Exceptions Handling
C# Exceptions Handling
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
C#.NET
C#.NETC#.NET
C#.NET
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 

Similar a 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 a 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#
 
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
 
C#ppt
C#pptC#ppt
C#ppt
 

Último

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach 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...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
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
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 

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