我似乎找不到一种方法不将对象添加到我的列表中是相同的,我试过.包含但我只是没有让它工作,任何其他解决方法?我相信我必须使用if语句,并在else中创建对象,但我如何在if语句中做?
public Form1()
{
InitializeComponent();
}
private const string filnamn = "kontakter.bin";
Kontakter kontakt;
private List<Kontakter> kontakter;
private void buttonLagra_Click(object sender, EventArgs e)
{
Kontakter nyKontakt = new Kontakter();
{
nyKontakt.Telefonnr = textBoxTelefonnr.Text;
nyKontakt.Namn = textBoxNamn.Text;
}
kontakter.Add(nyKontakt);
listBox1.DataSource = null;
listBox1.DataSource = kontakter;
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream(filnamn, FileMode.OpenOrCreate, FileAccess.Write);
bf.Serialize(fs, kontakter);
fs.Close();
}
Run Code Online (Sandbox Code Playgroud) c# ×1