C # cheat sheet for Java engineers

background

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.

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, 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

Access modifier

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.

Inheritance

Use : (colon) when inheriting. Add virtual to the method you want to override, and add ʻoverrideto override it. When you want to prohibit overriding, addfinal 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 you forget to add virtual or override

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

if statement

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

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

for statement

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);
}

while statement

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);

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 #

Recommended Posts

C # cheat sheet for Java engineers
Java cheat sheet
Java Stream API cheat sheet
Competitive programming private cheat sheet (Java)
javac, jar, java command cheat sheet
A cheat sheet for Java experienced people to learn Ruby (rails)
Getting Started with Ruby for Java Engineers
Technology for reading source code (cheat sheet)
[Java] Data type / string class cheat sheet
For JAVA learning (2018-03-16-01)
JMeter cheat sheet
2017 IDE for Java
Kotlin cheat sheet
[Docker cheat sheet]
Java for statement
For Java engineers starting Kotlin from now on
An introduction to Groovy for tedious Java engineers
[Java] for statement, while statement
Mockito + PowerMock cheat sheet
[Java] Package for management
Eclipse Collections cheat sheet
[Java] for statement / extended for statement
Rails Tutorial cheat sheet
Spring Boot2 cheat sheet
Java programming exercises for newcomers unpopular with active engineers
Countermeasures for Java OutOfMemoryError
SCSS notation cheat sheet
NLP for Java (NLP4J) (2)
(Memo) Java for statement
NLP for Java (NLP4J) (1)
What Java engineers need to prepare for the Java 11 release
Docker command cheat sheet
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
Books used for learning Java
Fastest Primality Test C # Java C ++
2018 Java Proficiency Test for Newcomers-Basics-
[Eclipse] Shortcut key cheat sheet
Java thread safe for you
Java for beginners, data hiding
[Java] Tips for writing source
Reproduce Java enum in C #
Java installation location for mac
C # and Java Overrides Story
Java application for beginners: stream
Java while and for statements
What Java Engineers Choose for Browser "Automatic Testing"? Selenium? Selenide? Geb?