How to create function which return var dictionary<int,string>

Keywords: c#


i would like to create function which would replace below code responsible for creating combobox items.

I would like to return var items = new Dictionary();

private void cboSection_SelectedIndexChanged(object sender, EventArgs e)

            var items = new Dictionary<int, string>();


            query = "sql question";
            SqlDataReader dr = sqlCon.DataReader(query);

            if (dr.HasRows) { cboWorkPlace.Enabled = true; }
            else { cboWorkPlace.Enabled = false; items.Add(0, "--Wybierz--"); }

            items.Add(0, "--Select--");

            while (dr.Read())
                items.Add(Int32.Parse(dr["IdPlace"].ToString()), dr["WorkPlace"].ToString());

            cboWorkPlace.DataSource = new BindingSource(items, null);
            cboWorkPlace.DisplayMember = "Value";
            cboWorkPlace.ValueMember = "Key";



You can just wrap it into a method like this.

private Dictionary<int, string> GetComboBoxDictionary()
    // Your code here

It seems like your code is missing some tuning though. There are statements which don't really make sense to me like the items.Clear at the end there and it's also not formattet well. I don't know if I should fix that or leave that up to you since it seems like this isn't the question.