0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VB → C# チートシート

Last updated at Posted at 2025-01-19

0. はじめに

Freeradicalの中の人、yamarahです。
VBからC#に移行しようって人のために、とりあえず必要になりそうなことを列挙しました。
これを読んで、あとはググれ!!

1. 基礎

C#では、行末に;を付けるのが基本。
後から出てきますが、VBでのThenEnd If間や、ForNext, SubEnd Subの間など、複数の行が一塊になっている部分は、{}で括ります。これを省略できる場合もありますが、ここでは触れません。

コメント

VB
' コメント
C#
// コメント

/* このように'/'と'*'の組合せで囲むと
複数行のコメントを作れます */

組み込み型

VB
Dim i As Interger ' 整数
Dim d As Double ' 実数
Dim str As String ' 文字列
Dim b As Boolean ' 真偽
C#
int i; // 整数
double d; // 実数
string str; // 文字列
bool b; // 真偽

変数に1足す。1引く。

VB
value = value + 1 ' 1足す
value = value - 1 ' 1引く
C#
value++; // 1足す
value--; // 1引く
// もしくは、
++value;
--value;
// でも、同じことができる。この場面では同じだけど、
// 使う場所によっては結果が変わることがある。

Nothing

VB
obj = Nothing
C#
obj = null;

名前空間の使用

VB
Imports System
C#
using System;

2. 条件分岐

慣習からか、VBでOrElseやAndAlsoを使っているのを私の周りでは見ないが、C#では条件を繋ぐ場合に一文字の|&を使うことは、あまり無い。

条件分岐(If文)

VB
If a = 0 OrElse (b <> 0 AndAlso b <> 1) Then
    ' 処理
ElseIf a = 1 Or (b <> 2 And b <> 3) Then
    ' 処理
Else
    ' 処理
End If
C#
if (a == 0 || (b != 0 && b != 1)) // ほぼコレ(||, &&)
{
    // 処理
}
else if (a == 1 | (b != 2 & b != 3)) // まず使わない(|, &)
{
    // 処理
}
else
{
    // 処理
}

条件分岐(Select-Case文)

VB
Select Case flag
  Case 0
    ' flag = 0の時の処理
  Case 1
    ' flag = 1の時の処理
  Case Else
    ' それ以外の時の処理
End Select
C#
switch(flag) {
  case 0:
    // flag == 0の時の処理
    break;
  case 1:
    // flag == 1の時の処理
    break;
  default:
    // それ以外の時の処理
    break;
}

三項演算子

VB
Console.WriteLine(If(value Mod 2 = 0, "even", "odd"))
C#
Console.WriteLine(value % 2 == 0 ? "even" : "odd");

Not

VB
If Not flag Then
    ' 処理
End if
C#
if (!flag)
{
    // 処理
}

3. ループ

カウンターを使ったForループ

VB
For counter as Interger = 0 To 10
  ' 処理
Next counter
C#
for (int counter = 0; counter <= 10; counter++) {
  // 処理
}

コンテナ内列挙のForループ

VB
For Each obj In container
  ' 処理
Next obj

' もしくは、例えばInteger型のcontainerの場合
Dim index as Integer
For Each index In container
  ' 処理
Next index
C#
foreach (var obj in container) {
  // 処理
}

// もしくは、例えばint型のcontainerの場合
int index;
foreach (index in container) {
  // 処理
}

Whileループ

VB
While value > 0
  ' 処理
Wend
C#
while (value > 0) {
 // 処理
}

ループのスキップ、中断

VB
For Each index In container
    If index < 0 Then
        Exit For
    ElseIf index >= 10 Then
        Continue For
    End If
    ' 処理
Next index
C#
foreach (var index in container)
{
    if (index < 0)
    {
        break;
    }
    else if (index >= 10) {
        continue;
    }
    // 処理
}

4. サブルーチン、関数

C#にはサブルーチンと関数の区別がない。サブルーチンの場合は、何も返さない(voidを返す)関数として定義する。

サブルーチン(引数なし)

VB
' 引数なし
Sub foo()
  ' 処理
End Sub

' 引数あり
Sub bar(value As Integer, Optional message as String = "Hello")
  If value <= 0 Exit Sub
  ' 処理
End Sub
C#
// 引数なし
void foo() {
  // 処理
}

// 引数あり
void bar(int value, string message = "Hello") {
  if (value <= 0) return;
  // 処理
}

関数

VB
Function foo(value As Integer) As Boolean
  ' 処理
  foo = True
End Function
C#
bool foo(int value) {
  // 処理
  return true;
}

5. Classの定義、オブジェクトの生成

Classの定義

VB
Public Class SomeClass
    Public Index As Integer
    Dim Name As String ' Private
End
C#
public class SomeClass
{
    public int Index;
    string Name; // private
}

オブジェクトの生成

VB
Dim values As New SomeClass
C#
SomeClass values = new();
// もしくは
var values = new SomeClass();

6. ジェネリック、ラムダ式

ジェネリック

VB
Dim values As List(Of Integer)
C#
List<int> values;

ラムダ式

VB
Dim evens = values.Where(Function(x) x Mod 2 = 0)
C#
var evens = values.Where(x => x % 2 == 0)

7. 例外処理

例外処理

VB
Try
    ' 処理
Catch ex As Exception
    ' エラー処理
Finally
    ' 後処理
End Try
C#
try
{
    // 処理
}
catch (Exception ex)
{
    // エラー処理
}
finally
{
    // 後処理
}
0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?