ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Add and retrieve Key Value from Generic List Collection in C# and VB.Net<p>Hi <a class="username" rel="yogesjoshi"> yogesjoshi</a>,</p> <p>Please refer below sample code.</p> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">public class Employee { public string Name { get; set; } public Dictionary&lt;string, string&gt; EmployeeDetails { get; set; } } private void Form1_Load(object sender, EventArgs e) { List&lt;Employee&gt; employees = new List&lt;Employee&gt;(); try { for (int i = 0; i &lt; 2; i++) { Employee emp = new Employee(); emp.Name = &#34;a&#34;; Dictionary&lt;string, string&gt; dict = new Dictionary&lt;string, string&gt;(); dict.Add(&#34;Department&#34;, &#34;IT&#34;); dict.Add(&#34;Mark&#34;, &#34;100&#34;); emp.EmployeeDetails = dict; employees.Add(emp); } string message = &#34;&#34;; for (int i = 0; i &lt; employees.Count; i++) { Dictionary&lt;string, string&gt; dic = employees[i].EmployeeDetails; foreach (KeyValuePair&lt;string, string&gt; keyValuePair in dic) { string name = employees[i].Name; string key = keyValuePair.Key.ToString(); string value = keyValuePair.Value.ToString(); message += &#34;Name = &#34; + name + &#34; | Key = &#34; + key + &#34; | Value = &#34; + value + Environment.NewLine; } } MessageBox.Show(message); } catch (Exception ex) { MessageBox.Show(ex.Message); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Public Class Employee Public Property Name As String Public Property EmployeeDetails As Dictionary(Of String, String) End Class Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load Dim employees As List(Of Employee) = New List(Of Employee)() Try For i As Integer = 0 To 2 - 1 Dim emp As Employee = New Employee() emp.Name = &#34;a&#34; Dim dict As Dictionary(Of String, String) = New Dictionary(Of String, String)() dict.Add(&#34;Department&#34;, &#34;IT&#34;) dict.Add(&#34;Mark&#34;, &#34;100&#34;) emp.EmployeeDetails = dict employees.Add(emp) Next Dim message As String = &#34;&#34; For i As Integer = 0 To employees.Count - 1 Dim dic As Dictionary(Of String, String) = employees(i).EmployeeDetails For Each keyValuePair As KeyValuePair(Of String, String) In dic Dim name As String = employees(i).Name Dim key As String = keyValuePair.Key.ToString() Dim value As String = keyValuePair.Value.ToString() message += &#34;Name = &#34; &amp; name &amp; &#34; | Key = &#34; &amp; key &amp; &#34; | Value = &#34; &amp; value &amp; Environment.NewLine Next Next MessageBox.Show(message) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><span style="text-decoration: underline;"><strong><img src="https://imgur.com/Cxrtuko.jpg" alt="" width="251" height="163" /></strong></span></p>https://www.aspforums.net:443/Threads/168220/Add-and-retrieve-Key-Value-from-Generic-List-Collection-in-C-and-VBNet/https://www.aspforums.net:443/Threads/168220/Add-and-retrieve-Key-Value-from-Generic-List-Collection-in-C-and-VBNet/Wed, 17 Oct 2018 01:40:40 GMT