準備
(なし)
デザイン
- フォーム (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)
{
if (Func1() & Func2())
listBox1.Items.Add("True and True");
else
listBox1.Items.Add("True and False");
listBox1.Items.Add("--------------");
if (Func1() && Func2())
listBox1.Items.Add("True and True");
else
listBox1.Items.Add("True and False");
}
private bool Func1()
{
listBox1.Items.Add("Func1");
return false;
}
private bool Func2()
{
listBox1.Items.Add("Func2");
return false;
}
}
}
解説
論理演算子で 2 つの結果を比較するとき、両方とも内容を確認する方法と、最初に出現した内容だけを確認する方法があります。C# と VC++/CLI では & と && により使い分けます。VB.NET では AND と AndAlso により使い分けます。また、同様に論理の場合も | と || や Or と OrElse により使い分けます。
結果
動作確認環境
Visual Studio 2022 Professional (.NET 7 C#11)
ログ
初版:2016.06.10 Visual Studio 2015 Professional (C# 6.0)
