準備
(なし)
デザイン
- フォーム (Form1) にボタン (button1) を配置します。
- フォーム (Form1) にリストボックス (listBox1) を配置します。
サンプルコード (C#)
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int v1 = 10;
int v2 = 3;
listBox1.Items.Add(v1 + v2);
listBox1.Items.Add(v1 - v2);
listBox1.Items.Add(v1 * v2);
listBox1.Items.Add(v1 / v2);
listBox1.Items.Add(v1 % v2);
listBox1.Items.Add(Math.Pow(v1, v2));
}
}
}
解説
上から順に、加算、減算、乗算、除算、除算の余り、べき乗を求めています。VB.NET のみ、べき乗の演算子がありますが、C# と C++/CLI にはこの演算子はありませんので、Math クラスの Pow メソッドを使用する必要があります。 C# と C++/CLI は同じ結果になりますが、VB.NET の結果は若干違います。
結果
動作確認環境
Visual Studio 2022 Professional (.NET 7 C#11)
ログ
初版:2016.03.22 Visual Studio 2015 Professional (C# 6.0)
