Вы находитесь на странице: 1из 29

C#

Java

, Java C#
Java. , :
, , , ,
C#.
Java C#, , C# , , C# ( ). , ,
Java, , Java.

Hello, World! Java:


public class Hello {
public static void main(String args []) {
System.out.println("Hello world! This is Java Code!");
}
}

C# :
using System;
public class Hello
{
public static void Main(string [] args)
{
System.Console.WriteLine("Hello world! This is C# code!");
}
}

1452 IX.
, , ,
. C#, Java, ,
( class). , , Java, C#
, Java. .
C# , Java. C#, , Main() ( ,
Java, M). ,
Main(). Java,
static
. Main() C# void, int. void , ,
int .
using C# import Java.
, C#, , C#- System. C# , .
, string s,
C# S String. ,
args Java , string
args C#. Java ,
. C# , ,
type[]. .
, , ,
. , Java
System.out.prinln().
C# System.Console.WriteLine().

C#
2 , , Java, C#
: (IL), .
C#, (, HelloWorld) .cs, IL, csc:
csc HelloWorld.cs

IL .
, ( Java java HelloWorld).
HelloWorld
Hello world! This is C# code!

. C# Java 1453


Java , , C# ( )
. .
, , com.samples,
package com.samples;. , , .
.
, Java , , . , com.samples
, com/samples. , Java:
package java2csharp.javasamples;
public class Hello {
public static void main(String args []) {
System.out.println("Hello world! This is Java Code!");
}
}

, . , JRE:
:
java java2csharp.javasamples.Hello

:
public class Referencer {
java2csharp.javasamples.Hello myHello =
new java2csharp.samples.Hello();

import , Referencer :
import java2csharp.javasamples.*;
public class Referencer {
Hello myHello = new Hello();
}

C#
namespace , . :
namespace java2csharp.csharpsamples
{
using System;
public class Hello
{
public static void Main(string [] args)
{
System.Console.WriteLine("Hello world! This is C# code!");
}
}
}

1454 IX.
,
(.), Java. , C# (*), Java using . Java:
, , . , : ,
,
, CLR . ,
, ( , Java); ;
, ,
, .
,
- . , ,
, :
namespace java2csharp.csharpsamples
{
using System;
public class Hello
{
public static void Main(string [] args)
{
System.Console.WriteLine("Hello world! This is C# code!");
}
}
}
namespace java2csharp.morecsharpsamples
{
using System;
public class AnotherHello
{
public static void OtherMain(string [] args)
{
System.Console.WriteLine("Hello again! This is more C# code!");
}
}
}

,

using. , System ( .NET)
. , (
), Java.
.
Java .

. C# Java 1455
, AnotherHello java2csharp.csharpsamples.hellosamples:
namespace java2csharp.csharpsamples
{
namespace hellosamples
{
using System;
public class AnotherHello
{
public static void Main(string [] args)
{
System.Console.WriteLine("Hello again! This is more C# code!");
}
}
}
}

Java ;
. C# .
, . . ,
Java, , C# .
; , (
.NET).
, . ,
public. . . Java
final abstract,
( ). .
, Java,
, (using
aliases). . . , Very.Very.Long.NameSpace.Name. ( VVLNN) :
using VVLNN = Very.Very.Long.Namespace.Name;


C# , Java, , . , (int) myInt, :
int myInt;

1456 IX.
, , , . , , C#, Java; C# , @. , ,
. ,
C#, Java . :
int
int
int
int
int

7x;
x7;
x;
x$;
@class;

int @7k;

//,
//,
//
//,
//, @
//
//, @


Java (camel-case) ,
. , .
Java .
, Java:
int id;
int idName;
int id_name;
//
final int CONSTANT_NAME;
//
int reallyLongId;
public class ClassName
// -
public interface InterfaceName
public void method(){}
public void myMethodName(){}

C#
C#.
C# .
,
. I. , :
int id;
int idName;
public class ClassName
public interface IInterfaceName
public void Method(){}
public void MyMethodName(){}

//
//
//
//

-
I

-

. C# Java 1457


Java C# : . , , , ,
. .


Java :
. C# . :
.
.
.
.


C# ( System), , ,
. , . C# Java .

C# ( Java), . .1.
.1. C# Java
C#

Java

Sbyte

8-

Byte

Short

16-

Short

int

32-

Int

long

64-

Long

byte

8-

ushort

16-

uint

ulong

, , : int, uint, long, ulong, decimal.


.

1458 IX.
52:
int dec = 52;
int hex = 0x34;
Console.WriteLine(" {0}, {1}",dec, hex);


char Unicode. C# , \x Unicode \u. , .
Java .

bool, Java, true false
, :
bool first_time = true;
bool second_time = (counter < 0);


C# decimal, 128- ,
1,010-28 to 7,91028. ,
(, , ).
, m. double.
double decimal, m :
decimal precise = 1.234m;
decimal precise = (decimal)1.234;


. .2 C# Java.
.2. C# Java
C#

Java

float

32-

float

double

64-

double

double, float.
double. float double, .
:
float f = 5.6;
Console.WriteLine(f);

. C# Java 1459
:
Literal of type double cannot be implicitly converted to type
'float'; use an 'F' suffix to create a literal of this type
double 'float';
'F'

.
float, .
F , float, double:
float f = 5.6F;

, D,
double.

, . Java
static final. , . .
:
interface
static
static
static
}

Color {
int RED = 0;
int GREEN = 1;
int BLUE = 2;

, ,
. .
Java, Singleton (), .
Java , :
final class Day { // final,
private String internal;
private Day(String Day) {internal = Day;} //
public static final Day MONDAY = new Day("MONDAY");
public static final Day TUESDAY = new Day("TUESDAY");
public static final Day WEDNESDAY = new Day("WEDNESDAY");
public static final Day THURDAY = new Day("THURSDAY");
public static final Day FRIDAY = new Day("FRIDAY");
}

, , . ,
final, ,
. private,
. ,
, .

1460 IX.
,
. , .
C# ,
. C#
enum. enum :
public enum Status
{
Working,
Complete,
BeforeBegin
}

0, 1 , Complete 1, BeforeBegin 2 . , , :
public enum Status
{
Working = 131,
Complete = 129,
BeforeBegin = 132
}


long, short byte. int , :
public enum Status : int
{
Working,
Complete,
BeforeBegin
}
public enum SmallStatus : byte
{
Working,
Complete,
BeforeBegin
}
public enum BigStatus : long
{
Working,
Complete,
BeforeBegin
}

, ,
, , . , byte C# . ,
SmallStatus 255 ; ,
255.

. C# Java 1461
, sizeof()
Status:
int x = sizeof(Status);
int y = sizeof(SmallStatus);
int z = sizeof(BigStatus);
Console.WriteLine("Regular size:\t{0}\nSmall size:\t{1}\nLarge size:\t{2}",
x, y, z);

:
Regular size: 4
Small size: 1
Large size: 8

C# (
struct) , , . Java
. ; , C#: (
4), , .
. ,
, , , . :
public struct EmployeeInfo
{
public string firstName
public string lastName
public string jobTitle
public string dept
public long employeeID
}

, ,
, .

:
EmployeeInfo employee1;
EmployeeInfo employee2;
employee1 = new EmployeeInfo();
employee1.firstName = "Dawn";
employee1.lastName = "Lane";
employee1.jobTitle = "Secretary";
employee1.dept = "Admin";
employee1.employeeID = 203;
employee2 = employee1;

:
.
:

1462 IX.
.
.
, , , .
.

( , 11 )
.
StructLayout System.Runtime.InteropServices,
, .
, C/C++. (union) ,
.
. , , ,
. , ; .
..
, :
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Explicit)]
public struct Variant
{
[FieldOffset(0)]public int intVal;
[FieldOffset(0)]public string strinVal;
[FieldOffset(0)]public decimal decVal;
[FieldOffset(0)]public float floatVal;
[FieldOffset(0)]public char charVal;
}

FieldOffset, ,
. 0 , , , . , decimal.


,
. .
, . , C# Java. ,
. C#.
C# Java . C# ,
. C++ ( ) 6.

. C# Java 1463


C# Java. C# ( Java):
int[] x = new int[20]; //same as in Java except [] must be next to type
int[,] y = new int[12,3]; //same as int y[][] = new int[12][3];
int[][] z = new int[5][]; //same as int x[][] = new int[5][];

C# ,
. Java [] ; . ,
. , x :
int xLength = x.Length;

, Sort():
Array.Sort(x);

, C# , . ,
System.Collections.ArrayList ( ArrayList
Java). C# 9.


Java , (). C#. C# , . .
,
, .
, .
,

.

Object. Object ,
. , C#:
int x = 10;
Object obj = x;

Java . , .
Object, , .

1464 IX.
Java. , . , ,
:
int x = 10;
Object obj = x;
int y = (int) obj;

C#, ,
. 5 .

. .3 C#.
.3. C#

+ - * / %

& | ^ ~ && || !

++ --

<< >>

== != < > <= >=

= += -= *= /= %= &= |= ^= <<= >>=

( )

( )

[]

()

( )

?:

new

sizeof ( ) is typeof as

checked unchecked

* -> & ( ) []

Java , C#
Java. .
,
, Java instanceof.
C# is. true, . :
string y = "a string";
object x = y;

. C# Java 1465
if(x is System.String)
{
System.Console.WriteLine("x is a string");
}

, Java ,
, sizeof. C#
. Java, . , struct enum.
sizeof. : sizeof(<ValueType>),
<ValueType> . , sizeof
(unsafe) . sizeof
.
typeof
System.Type . Java , Class, . typeof
. sizeof, .
typeof(<Type>), <Type> ,
.


Java. :
if...else if...else
if(option == 1)
{
// -
}
else if(option == 2)
{
// -
}
else
{
// ,
}
switch
switch(option)
{
case 1:
// -
break;
case 2:
// -
break;
default:
break;
}

1466 IX.
, C# switch ( Java)
. case break, . case ,
goto.
for
for (int i = 0; i <10; i++)
{
// 10
}
while
bool condition = false;
while (!condition)
{
// -, condition
}
do...while
bool condition;
do
{
// -, condition
// , , ,
// condition
} while (condition);
foreach

C# foreach,
. . foreach :
foreach (ItemType item in TargetCollection)

ItemType , ,
TargetCollection . ,
foreach, . . :
, .
GetEnumerator(),
. , , , .
, GetEnumerator(). :
MoveNext().
MoveNext() true, .
MoveNext() .
Current, ItemType ( , ItemType).
Current .

. C# Java 1467
C# foreach
Hashtable:
Hashtable t = new System.Collections.Hashtable();
t["a"] = "hello";
t["b"] = "world";
t["c"] = "of";
t["d"] = "c-sharp";
foreach(System.Collections.DictionaryEntry b in t)
{
Console.WriteLine( b.Value );
}

break switch;
. continue
, return , .

C# Java . , , ,
.
. C# , ( ) , Java. C# Java. ,
( ).


Java, , . . .4 C# Java.
.4. C# Java

Java

public

public

. , , public .

private

private

. private .

internal

protected

. C# protected ,
Java. protected
.

protected
internal

Protected

1468 IX.
private , .
.
public / . C# Java
protected- . Java protected , C# protected
.
C# internal.
internal , , .
Java ( ) internal C# , internal , .


, C# Java
. ,
. .5.
.5. C# Java

Java

virtual

-
( Java ).

static

static

, static, ,
. , .

event


. event
, . , , , .

abstract

abstract

,
. - ,
override.

const

final

, . Java const,
.

. C# Java 1469

. .5

Java

readonly

, .

extern

, .
DllImport.

partial

, .

override

, , .

6.
Java, C# , .
C# native,
transient, volatile synchronized. Java native , , .
, , .
extern. extern , ( DLL-, ). Java, , abstract extern.
Flower extern:
public class Flower
{
public Flower(){}
public extern int GetColor();
// Flower
}

DllImport . , See(), User32.dll:


public class Flower
{
public Flower(){}
[System.Runtime.InteropServices.DllImport ("User32.dll")]
public static extern int GetColor();
// Flower
}

, GetColor() static.
DllImport , .

1470 IX.


Java C# , ,
. , C# , . in ( ), ref out. :
public static void Main(string[] args)
{
int a = 10;
Console.WriteLine(a);
AddOne(a);
Console.WriteLine(a);
}
public static void AddOne(int a)
{
a++;
}

( C#, Java):
10
10

a ,
a Main(). , a AddOne()
a Main(). , , ; , , a .
, , :
public static void Main(string[] args)
{
int a = 10;
Console.WriteLine(a);
AddOne(ref a);
Console.WriteLine(a);
}
public static void AddOne(ref int a)
{
a++;
}

:
10
11

, ref. ,
out. , out
, .
100:

. C# Java 1471
public static void Main(string[] args)
{
int a;
Add(out a);
Console.WriteLine(a);
}
public static void Add(out int a)
{
a = 100;
}

Java, C# get set


.
, . get (get
accessor), ,
set (set accessor), . value , .
( set),
( get). , Person,
Age Name:
public class Person
{
private int age;
private string name;
public Person(string name)
{
this.name = name;
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public string Name
{
get
{
return name;
}
}
}

1472 IX.
Age get set, . Name ;
Name. , (public) :
Person john = new Person(" ");
john.Age = 30;
Console.WriteLine(" {0}, {1} .", john.Name, john.Age);

:
, 30 .

, .

C# , C++. Java, .
(~):
~Sample()
{
}

: .NET , .
, , .
, ,
. close()
, .


C# Java.
( ,
) ( , ).
C# extends implements,
Java. C#, (:). , , . interface
. :
//
class MyBaseClass
{
//
}
// IFirstInterface
interface IFirstInterface
{

. C# Java 1473
//
}
// MyBaseClass,
class MySubClass : MyBaseClass, IFirstInterface, ISecondInterface
{
//
}


Java, C# abstract , , ( ) . ,
, , sealed.


C# sealed
, , sealed, .
final ( Java) .
final , . final, , ; (
, ,
.)


this Java C#. Java super . C#
base. C# CalculateFor, x,
power (, x, , x,
x x); , :
using System;
public class CalculateFor
{
internal int x;
public CalculateFor(int x)
{
this.x = x;
}
public int ToThePower(int power)
{
int total = 1;
for(int i = 0; i < power; i ++)
{
total *= x;
}
return total;
}
}

1474 IX.
, 9 power
3:
CalculateFor myNumber = new CalculateFor(9);
int result = myNumber.ToThePower(3);

CalculateFor ExpCalculateFor,
ToTheExponent(), :
using System;
public class ExpCalculateFor
{
internal float y;
public ExpCalculateFor(float y) : CalculateFor(10)
{
this.y = y;
}
public int ToTheExponent(int power)
{
int total = 1;
for(int i = 0; i < power; i ++)
{
total *= base.x;
}
total *= y;
return total;
}
}

, .
ToTheExponent() , ToTheExponent() :
public int ToTheExponent(int power)
{
float total = (base.x).(base.ToThePower(power));
total *= y;
return total;
}


C# . , Java,
, . C# virtual override. ,
-,
virtual. ,
override. :

. C# Java 1475
using System;
public class FruitPlant
{
public FruitPlant(){}
public virtual void BearFruit()
{
Console.WriteLine("Generic fruit plant");
}
}
class MangoTree : FruitPlant
{
public MangoTree(){}
public MangoTree(){}
public override void BearFruit()
{
Console.WriteLine("Tree fruit is:->Mango");
}
}
public class FruitPlantTest
{
public FruitPlantTest(){}
public static void Main(string[] args)
{
FruitPlant p = new FruitPlant();
p.BearFruit();
MangoTree t = new MangoTree();
t.BearFruit();
((FruitPlant)t).BearFruit();
}
}

:
Generic fruit plant
Tree fruit is:->Mango
Tree fruit is:->Mango

, Fruit(), , MangoTree Plant. , ,


.
C#, virtual, C# .

,
virtual. , C# new.
, . , :
public class FruitPlant
{
public FruitPlant(){}
public void BearFruit()

1476 IX.
{
Console.WriteLine("Generic fruit plant");
}
}
class MangoTree : FruitPlant
{
public MangoTree(){}
new public void BearFruit()
{
Console.WriteLine("Tree fruit is:->Mango");
}
}
// - FruitPlantTest

:
Generic plant fruit
Tree fruit is:->Mango
Generic plant fruit

, , , . MangoTree Plant
BearFruit(). Plant.
, new .


- Java. Java
java.io.BufferedReader, System.in
. Java JavaEcho, ,
Java.io
:
import java.io.*;
public class JavaEcho {
public static void main(String[] args)throws IOException {
BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in));
String userInput = stdin.readLine ();
System.out.println ("You said: " + userInput);
}
}

C# System.Console
. ; Console ,
, , . Console .6 .7.

. C# Java 1477
.6. Console

Error


TextWriter.

In


TextReader.

Out


TextWriter.

.7. Console

OpenStandardError()

. Stream.

OpenStandardInput()

. Stream.

OpenStandardOutput()

. Stream.

Read()

ReadLine()

string Console, .

SetError()

Error
TextWriter.

SetIn()

In
TextReader.

SetOut()

Out TextWriter.

Write()

. Console.Out.

WriteLine()

. Console.Out.

Console , ( ) System.Console.
Console,
JavaEcho C# :
class CSEchoer
{
static void Main(string[] args)
{
string userInput = System.Console.ReadLine();
System.Console.WriteLine ("You said : " + userInput);
}
}

1478 IX.
, Java.
Console.WriteLine()
.
, . EchoGame:
class EchoGame
{
static void Main(string[] args)
{
System.Console.WriteLine(" ?");
string userInput1 = System.Console.ReadLine();
System.Console.WriteLine(" ?");
string userInput2 = System.Console.ReadLine();
System.Console.WriteLine(" ?");
string userInput3 = System.Console.ReadLine();
System.Console.WriteLine(" , ?");
string userInput4 = System.Console.ReadLine();
System.Console.WriteLine(" ?");
string userInput5 = System.Console.ReadLine();
System.Console.WriteLine(" "
+ " {0}, - {1},\n" +
", {2}, {3} "
+ "{4} ! ", userInput1, userInput2,
userInput3, userInput4, userInput5 );
}
}

, {0}, ( userInput1).
, , . , WriteLine(), , .
, , , . , , .
, . , , {1}
, . {0}
strA, {2} strC:
Console.WriteLine("hello {0} {1} {2}", strA, strB, strC);

Microsoft C# , , C C++. Java C++, , C#,


Java.

. C# Java 1479
C# .NET Framework, , , , (Common Type System CTS). Java
C C++, . , .
C#
. C#
, Java, , , Java. C# , ,
, Java.
C#
, . Java. C# , , , ,
, , . C#
in ( ), out ref.
C#
virtual override. C#
get() set()
.