準備
(なし)
デザイン
- フォーム (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[] iPrice1 = { 100, 200, 300 };
int[] iPrice2 = Array.ConvertAll(iPrice1, TaxCalc);
foreach (int item in iPrice2)
{
listBox1.Items.Add(item);
}
}
private int TaxCalc(int iPrice)
{
return (int)(iPrice * 1.1);
}
}
}
解説
配列 iPrice に格納されている税抜金額から税込金額を計算して、配列 Price2 に格納しています。ConvertAll メソッドを使用すると、配列をループ処理することなく、計算処理用のメソッドを呼び出し、一括処理することができます。
結果
動作確認環境
Visual Studio 2022 Professional (.NET 7 C#11)
ログ
初版:2016.02.20 Visual Studio 2015 Professional (C# 6.0)
