|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TestProgram;
namespace TestProgram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Dictionary<String, Student> myStudent = new Dictionary<String, Student>();
for (int i = 1; i <= 3; i++)
{
Student st = new Student();
st.id = "ID"+i;
st.name = "Name_" + i;
myStudent.Add("Student" + i, st);
}
//Loop over Dictionary with foreach
foreach (var student in myStudent)
{
MessageBox.Show("Key => " + student.Key + " ,Student ID => " + student.Value.id + " ,Student Name => " + student.Value.name);
}
//Get from the specified key name
Student std = new Student();
if (dict.ContainsKey("Student1"))
{
std = myStudent["Student1"];
MessageBox.Show(std.id+" "+std.name);
}
}
}
}
|