Ответы
Класс A:
using System.Text;
namespace WinFormsApp1
{
internal class A
{
public delegate void GetLogString(string operationName, StringBuilder logContainer);
private decimal a = 3;
private decimal b = 5;
public decimal C1 => a++;
public decimal C2 => b++;
public A() { }
public decimal D1()
{
return C1 * C1;
}
public decimal D2()
{
return C2 * C2;
}
public void AddALog(string operationName, StringBuilder logContainer)
{
string log = $"after {operationName} {nameof(a)}={a}\t";
logContainer.Append(log);
}
public void AddBLogString(string operationName, StringBuilder logContainer)
{
string log = $"after {operationName} {nameof(b)}={b}\t";
logContainer.Append(log);
}
}
}
Форма:
using System.Text;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
private readonly A _a;
private readonly A.GetLogString _c1Log;
private readonly A.GetLogString _c2Log;
private readonly A.GetLogString _fullLog;
public Form1()
{
InitializeComponent();
_a = new A();
_c1Log = _a.AddALog;
_c2Log = _a.AddBLogString;
_fullLog = _c1Log + _c2Log; // объединение
}
private void PrintC1Btn_Click(object sender, EventArgs e)
{
var c1 = _a.C1;
var logContainer = new StringBuilder();
_c1Log(nameof(_a.C1), logContainer);
InfoListBox.Items.Add(logContainer.ToString());
}
private void PrintC2Btn_Click(object sender, EventArgs e)
{
var c2 = _a.C2;
var logContainer = new StringBuilder();
_c2Log(nameof(_a.C2), logContainer);
InfoListBox.Items.Add(logContainer.ToString());
}
private void PrintFullLogBtn_Click(object sender, EventArgs e)
{
var c1 = _a.C1;
var c2 = _a.C2;
var logContainer = new StringBuilder();
_fullLog($"{nameof(_a.C1)},{nameof(_a.C2)}", logContainer);
InfoListBox.Items.Add(logContainer.ToString());
}
}
}
![](https://st.uroker.com/files/f2f/f2fe52dc8c7fe05b47dfe13e65802771.png)