変数 宣言C# 入門e
変数の宣言
データ型 変数名;
int i;
vbだと
Dim i As Integer
書くのが楽。
intとIntegerは厳密には違うけど。
変数の代入
変数名 = 値;
i=0;
代入は同じですね。
;つけるぐらい。
宣言して代入
int i=0;
c#は文末に;を書きます。
サンプルソース
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int i;
i = 0;
int j = 2;
}
}
}
C#3.0からは
var が使用できます。
var 変数名 = 値;
var i = 1;
何かというと型推論を自動で
宣言時にしてくれます。
なので初期化する必要があります。
✖ var i;
何でも入るのはなく、自動的に値により型を決めてくれます。