[Java] C# cheat sheet for Java engineers

4 minute read

background

I’ve been working mainly on Java until now, but I decided to use C#, which I barely touched, for work, so I studied it. Although there are some similarities, there are differences in thoughts and subtle differences in writing, so I thought that “how would I write this?” will definitely occur, so I picked up the places that I would often use and cheat sheet. I summarized it as. I will know the details in detail.

Declaration of package to use

Java

The calling method is different.

import java.util.ArrayList;

C#

using System.Collections.Generic;

Package definition

The declaration is different. Also, the idea is different, so be careful when naming.

Java

package StudyPackage.com.anyplus

C#

namespace StudyPackage
// There is no domain because the idea is different

Access modifier

There are internal and protected internal that Java does not have. Also note that the accessible range differs depending on the thought even with the same qualifier.

Qualifiers Java C#
public accessible from anywhere accessible from anywhere
protected accessible from the same package or a derived class accessible from a derived class
internal Does not exist Can be accessed within the same assembly (DLL)
protected internal does not exist accessible only in the same assembly (DLL) or from a type derived from the containing class
private accessible only from within the same class accessible only from within the same class
Not specified
(default)
Can be accessed in 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 it is the same project if it is within the solution of Visual Studio.

Inheritance

Use : (colon) when inheriting. Attach virtual to the method you want to override, and attach override when overriding. When you want to prohibit overriding, add final to the method in Java, but add sealed in C#. (Updated July 21, 2020)

Java

class Program {
    public static main(string[] args) {
        SubA cl = new SubA();
        cl.printStr1(); // SubA's printStr1
    }
}

class Base {
    public void printStr1(){
        System.out.println("Base printStr1");
    }
}

class SubA extends Base {
    public void printStr1(){
        System.out.println("SubA's printStr1");
    }
}
class SubA extends Base {
    public final void printStr2(){ // This method cannot be overridden
        System.out.println("SubA's printStr1");
    }
}

C#

class Program
{
    static void Main(string[] args)
    {
        SubA cl = new SubA();
        cl.printStr1(); // SubA's printStr1
    }
}

class Base {
    public virtual void printStr1(){
        Console.WriteLine("It is printStr1 of Base");
    }
}

class SubA :Base {
    public override void printStr1(){
        Console.WriteLine("SubA's printStr1");
    }
}
class SubA :Base {
    public override sealed void printStr1(){ // This method cannot be overridden
        Console.WriteLine("SubA's printStr1");
    }
}

If you forget to add virtual or override

If you do not add virtual or override, they will not be overridden. Reference site The result is as follows. (Updated July 21, 2020)

// 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 is not 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");
         }
    }
}

if statement

No difference. String comparison has the difference that it is possible to compare values using the equality operator.

Java

String strVal = "";

if("str".equals(strVal)){
    System.out.println("Same");
} else
{
    System.out.println("No");
}

C#

string strVal = "";

if("str" == strVal){ // Supplement
    Console.WriteLine("Same");
} else
{
    Console.WriteLine("No");
}

** Supplement ** Since the String type is a reference type, comparing with the equality operator in Java makes a comparison as to whether the same instance is being referenced, but in the case of C#, since string.equals is called behind the scenes, the value comparison is also possible with this writing method. .. There is also a method that can specify a comparison method called String.equals(String, StringComparison), so I felt that it was safe to use this when writing code… But if the same thing is done, the code amount Is the equality operator with less number better? (Updated July 17, 2020)

Reference → String.Equals method-.NET Tips|dobon.net

switch statement

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("Invalid value.");
    break;
}

for statement

Java extended 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#```c#

for(int i = 0; i <= 10; i++){ Console.WriteLine(str); }

string[] strArgs = {“1”, “2”}; foreach(string str in strArgs){ Console.WriteLine(str); }


# while statement
There is no difference between the pre-judgment and the post-judgment.

## Java
```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);

reference

[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#