Java
|
C#
|
Metodo Main
|
class B{
public static void main(String[] args){
System.out.println(«Hello World»);
}
}
|
using System;
class A{
public static void Main(String[] args){
Console.WriteLine(«Hello World»);
}
}
|
Herencia
|
class B extends A implements Comparable{
int compareTo(){}
public static void main(String[] args){
System.out.println(«Hello World»);
}
}
|
using System;
class B:A, IComparable{
int CompareTo(){}
public static void Main(String[] args){
Console.WriteLine(«Hello World»);
}
}
|
Identificacion de tipos durante la ejecucion
|
if(x instanceof MyClass)
MyClass mc = (MyClass) x;
|
if(x is MyClass)
MyClass mc = (MyClass) x;
|
Espacio de nombres
|
package com.carnage4life;
public class MyClass {
int x;
void doStuff(){}
}
|
namespace com.carnage4life{
public class MyClass {
int x;
void doStuff(){}
}
}
|
Constructors, Destructors y Finalizers
|
public class MyClass {
static int num_created = 0;
int i = 0;
MyClass(){
i = ++num_created;
System.out.println(«Created object #» + i);
}
public void finalize(){
System.out.println(«Object #» + i + » is being finalized»);
}
public static void main(String[] args){
for(int i=0; i < 10000; i++)
new MyClass();
}
}
|
using System;
public class MyClass {
static int num_created = 0;
int i = 0;
MyClass(){
i = ++num_created;
Console.WriteLine(«Created object #» + i);
}
~MyClass(){
Console.WriteLine(«Object #» + i + » is being finalized»);
}
public static void Main(string[] args){
for(int i=0; i < 10000; i++)
new MyClass();
}
}
|
Sincronizacion de bloques
|
public void withdrawAmount(int num){
synchronized(this){
if(num < this.amount)
this.amount -= num;
}
}
|
public void WithdrawAmount(int num){
lock(this){
if(num < this.amount)
this.amount -= num;
}
}
|
Metodos sincronizados
|
public class BankAccount{
public synchronized void withdrawAmount(int num){
if(num < this.amount)
this.amount – num;
}
}//BankAccount
|
using System;
using System.Runtime.CompilerServices;
public class BankAccount{
[MethodImpl(MethodImplOptions.Synchronized)]
public void WithdrawAmount(int num){
if(num < this.amount)
this.amount – num;
}
}//BankAccount
|
Modificadores de acceso
|
|
private
|
private
|
public
|
public
|
internal
|
internal
|
|
protected
|
|
internal protected
|
* en c# por defecto private en java protected
|
Declarar constantes
|
|
import java.util.*;
public class ConstantTest{
/* Contanste de tiempo de compilacion*/
final int i1 = 10; //Instancia de una variable
static final int i2 = 20; //variable de clase
/* contantes de ejecucion */
public static final long l1 = new Date().getTime();
/* Referencia de objeto como constante */
final Vector v = new Vector();
/* final sin inicializar */
final float f;
ConstantTest() {
// Variables finales tienen que ser iniciadas en el constructor
f = 17.21f;
}
}
|
using System;
public class ConstantTest{
/* Contanste de tiempo de compilacion*/
const int i1 = 10; //implicitly a static variable
// no compila por el codigo
// public static const int i2 = 20;
/* run time constants */
public static readonly uint l1 = (uint) DateTime.Now.Ticks;
/* object reference as constant */
readonly Object o = new Object();
/* variable de solo lectura sin iniciar */
readonly float f;
ConstantTest() {
// unitialized readonly variable must be initialized in constructor
f = 17.21f;
}
}
|
Tipos Primitivos
|
Todos los tipos primitivos de java tienen su equivalencia en c# con el mismo nombre, salvo byte que es equivalente a sbyte de c#.
|
Aparte de los tipos primitivos c# tiene versiones sin signo de ulong, uint, ushort y byte. El unico tipo de C# que no hay en java es decimal el cual guarda los numeros sin errores de redondeo
|
Array
|
int[] iArray = new int[100]; //valid, iArray is an object of type int[]
float fArray[] = new float[100]; //valid, but isn’t clear that fArray is an object of type float[]
|
int[] iArray = new int[100]; //valid, iArray is an object of type int[]
float fArray[] = new float[100]; //ERROR: Won’t compile
|
Llamando al constructor base y cadenas de construcctores
|
class MyException extends Exception{
private int Id;
public MyException(String message){
this(message, null, 100);
}
public MyException(String message, Exception innerException){
this(message, innerException, 100);
}
public MyException( String message,Exception innerException, int id){
super(message, innerException);
this.Id = id;
}
}
|
using System;
class MyException: Exception
{
private int Id;
public MyException(string message): this(message, null, 100){ }
public MyException(string message, Exception innerException):
this(message, innerException, 100){ }
public MyException(string message, Exception innerException, int id):
base(message, innerException){
this.Id = id;
}
}
|
Copiar un archivo
|
import java.io.*;
public class FileIO{
public static void main(String[] args) throws IOException {
File inputFile = new File(«input.txt»);
File outputFile = new File(«output.txt»);
FileReader in = new FileReader(inputFile);
BufferedReader br = new BufferedReader(in);
FileWriter out = new FileWriter(outputFile);
BufferedWriter bw = new BufferedWriter(out);
String str;
while((str = br.readLine())!= null)
bw.write(str);
br.close();
bw.close();
}
}//FileIOTest
|
using System;
using System.IO;
public class FileIOTest {
public static void Main(string[] args){
FileStream inputFile = new FileStream(«input.txt», FileMode.Open);
FileStream outputFile = new FileStream(«output.txt», FileMode.Open);
StreamReader sr = new StreamReader(inputFile);
StreamWriter sw = new StreamWriter(outputFile);
String str;
while((str = sr.ReadLine())!= null)
sw.Write(str);
sr.Close();
sw.Close();
}
}//FileIOTest
|
|
|
Pingback: Diferencia entre Java y C# - PcNoticias.com
Hola! Un gran documento, de mucha ayuda para los que nos iniciamos en C#! Gracias!!
Muy buena info, justo lo que quería jaja, gracias.
exelent informacion me sirve de mucho