Java
Program Structure
C#
package hello;
public class HelloWorld {
public static void main(String[] args) {
String name = "Java";
// See if an argument was passed from mand line
if ( == 1)
name = args[0];
("Hello, " + name + "!");
}
}
using System;
namespace Hello {
public class HelloWorld {
public static void Main(string[] args) {
string name = "C#";
// See if an argument was passed from mand line
if ( == 1)
name = args[0];
("Hello, " + name + "!");
}
}
}
Java
Comments
C#
// Single line
/* Multiple
line */
/** Javadoc ments */
// Single line
/* Multiple
line */
/// ments on a single line
/** ments on multiple lines */
Java
Data Types
C#
Primitive Types
boolean
byte
char
short, int, long
float, double
Reference Types
Object
Value Types
bool
byte, sbyte
char
short, ushort, int, uint, long, ulong
float, double, decimal
structures, enumerations
Reference Types
object
(superclass of all other classes)
String
arrays, classes, interfaces
Conversions
// int to String
int x = 123;
String y = (x); // y is "123"
// String to int
y = "456";
x = (y); // x is 456
// double to int
double z = ;
x = (int) z; // x is 3 (truncates decimal)
(superclass of all other classes)
string
arrays, classes, interfaces, delegates
Convertions
// int to string
int x = 123;
String y = (); // y is "123"
// string to int
y = "456";
x = (y); // or x = (y);
// double to int
double z = ;
x = (int) z; // x is 3 (truncates decimal)
Java
Constants
C#
// May be initialized in a constructor
final double PI = ;
const double PI = ;
// Can be set to a const or a variable. May be initialized in a constructor.
readonly int MAX_HEIGHT = 9;
J
Java and C# Comparison - Java 与 C# 语法对比总结 来自淘豆网m.daumloan.com转载请标明出处.