I've mainly worked in Java so far, but I decided to use C #, which I rarely touched on, so I studied. There are some similarities, but there are differences in ideas and subtle differences in writing, so I thought that "how should I write this?" Will definitely occur, so I picked up the places that I think I will use often and cheat sheets I tried to summarize as. I will know the details one by one.
Java The calling method is different.
import java.util.ArrayList;
C#
using System.Collections.Generic;
The declaration is different. Also, be careful about naming because the idea is different.
Java
package StudyPackage.com.anyplus
C#
namespace StudyPackage
//There is no domain because the idea is different
There are ʻinternal and
protected internal` that Java does not have.
Also note that the accessible range differs depending on the idea even with the same modifier.
Modifier | Java | C# |
---|---|---|
public | Accessable from anywhere | Accessable from anywhere |
protected | Accessable from the same package or derived classes | Accessable from derived classes |
internal | not exist | Same assembly(DLL)Accessable within |
protected internal | not exist | Same assembly(DLL)Only accessible within or from types derived from the stored class |
private | Only accessible from within the same class | Only accessible from within the same class |
unspecified (default) |
Can be accessed within the same package | Same treatment as private |
** What is the same assembly (DLL)? ** The same assembly is the same exe file or DLL file. It seems that they are the same project within the Visual Studio solution.
Use :
(colon) when inheriting.
Add virtual
to the method you want to override, and add ʻoverrideto override it. When you want to prohibit overriding, add
final to the method in Java, but add
sealed` in C #. (Updated on 2020.07.21)
Java
class Program {
public static main(string[] args) {
SubA cl = new SubA();
cl.printStr1(); //SubA printStr1
}
}
class Base {
public void printStr1(){
System.out.println("It is printStr1 of Base");
}
}
class SubA extends Base {
public void printStr1(){
System.out.println("SubA printStr1");
}
}
class SubA extends Base {
public final void printStr2(){ //This method cannot be overridden
System.out.println("SubA printStr1");
}
}
C#
class Program
{
static void Main(string[] args)
{
SubA cl = new SubA();
cl.printStr1(); //SubA printStr1
}
}
class Base {
public virtual void printStr1(){
Console.WriteLine("It is printStr1 of Base");
}
}
class SubA : Base {
public override void printStr1(){
Console.WriteLine("SubA printStr1");
}
}
class SubA : Base {
public override sealed void printStr1(){ //This method cannot be overridden
Console.WriteLine("SubA printStr1");
}
}
If virtual and override are not added, they will not be overridden. Reference site The result is as follows. (Updated on 2020.07.21)
// https://wandbox.org/permlink/bE71rRgU8ZLTktBS
namespace Wandbox
{
class Program
{
static void Main(string[] args)
{
SubClass c1 = new SubClass();
c1.Method1(); // Class Method1
c1.Method2(); // Class Method2
BaseClass c2 = new SubClass();
c2.Method1(); //Base Method1 * Because it has not been overridden
c2.Method2(); // Class Method2
}
}
public class BaseClass
{
public void Method1()
{
Console.WriteLine("Base Method1");
}
public virtual void Method2()
{
Console.WriteLine("Base Method2");
}
}
public class SubClass : BaseClass
{
public void Method1()
{
Console.WriteLine("Class Method1");
}
public override void Method2()
{
Console.WriteLine("Class Method2");
}
}
}
No difference. The difference is that string comparisons can be compared with equality operators.
Java
String strVal = "";
if("str".equals(strVal)){
System.out.println("It's the same");
} else
{
System.out.println("No");
}
C#
string strVal = "";
if("str" == strVal){ //Supplement
Console.WriteLine("It's the same");
} else
{
Console.WriteLine("No");
}
** Supplement **
Since the String type is a reference type, if you compare it with the equality operator in Java, it will be a comparison whether it refers to the same instance, but in the case of C #, since string.equals is called behind the scenes, it is possible to compare values with this writing method as well. ..
There is also a method called String.equals (String, StringComparison)
that allows you to specify a comparison method, so I felt it was safe to use this when writing code ... but if the same thing is done, the amount of code Is the equality operator with less of better? (Updated on 2020.07.17)
Reference → String.Equals method --.NET Tips | dobon.net
No difference
Java
int month = 7;
switch (month)
{
case 1:
case 2:
case 3:
System.out.println("1Q");
break;
case 4:
case 5:
case 6:
System.out.println("2Q");
break;
case 7:
case 8:
case 9:
System.out.println("3Q");
break;
case 10:
case 11:
case 12:
System.out.println("4Q");
break;
default:
System.out.println("Illegal value.");
break;
}
C#
int month = 7;
switch (month)
{
case 1:
case 2:
case 3:
Console.WriteLine("1Q");
break;
case 4:
case 5:
case 6:
Console.WriteLine("2Q");
break;
case 7:
case 8:
case 9:
Console.WriteLine("3Q");
break;
case 10:
case 11:
case 12:
Console.WriteLine("4Q");
break;
default:
Console.WriteLine("Illegal value.");
break;
}
The Java extension for statement is foreach in C #.
Java
for(int i = 0; i <= 10; i++){
System.out.println(str);
}
String[] strArgs = {"1", "2"};
for(String str : strArgs){
System.out.println(str);
}
C#
for(int i = 0; i <= 10; i++){
Console.WriteLine(str);
}
string[] strArgs = {"1", "2"};
foreach(string str in strArgs){
Console.WriteLine(str);
}
There is no difference between pre-judgment and post-judgment.
Java
int i = 0;
while (i < 5) {
System.out.println(i); //0,1,2,3,4 is output
i++;
}
do {
System.out.println(i); //0,1,2,3,4 is output
i++;
} while (i < 5);
C#
int i = 0;
while (i < 5) {
Console.WriteLine(i); //0,1,2,3,4 is output
i++;
}
do {
Console.WriteLine(i); //0,1,2,3,4 is output
i++;
} while (i < 5);
[Comparison of C # and Java](http://www.yo.rim.or.jp/~nag/cgi-bin/wiki.cgi?p=C%23%A4%C8Java%A4%CE%C8%E6 % B3% D3) Introduction to C #
Recommended Posts