C#.NET Collection
In collections based on IList or directly on ICollection, every element contains only a value. These types include:
1. Array
2. ArrayList
3. List<T>
4. Queue
5. ConcurrentQueue<T>
6. Stack
7. ConcurrentStack<T>
8. LinkedList<T>
In collections based on the IDictionary interface, every element contains both a key and a value. These types include:
1. Hashtable
2. SortedList
3. SortedList<TKey,TValue>
4. Dictionary<TKey,TValue>
5. ConcurrentDictionary<TKey,TValue>
//-----Example-------
//1. List
//2. Dictionary
//3. Stack
//4. Queue
//5. Table
//6. ArrayList
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Text.Json;
List
//--------String List----------
List<string> customers = new List<string>();
customers.Add("Mr. Jahid");
customers.Add("Mr. Rasel");
customers.Add("Abdur Rahman");
foreach (string customer in customers)
{
Console.WriteLine(customer);
}
//---------Object list: Book--------
public class Book
{
//---Properties of Book -
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public double Price { get; set; }
public string isbn { get; set; }
}
List<Book> books = new List<Book>() {
new Book()
{
Id = 1,
Title = "Introducing Java",
Author="Mr. Abc",
isbn ="4343553"
},new Book{
Id = 2,
Title = "Introducing C++",
Author = "Mr. Efg",
isbn = "343434"
},new Book
{
Id = 3,
Title="Introducing PHP",
Author="Mr. Gef",
isbn="343433"
}
};
var jsonBook = JsonSerializer.Serialize(books);
Console.WriteLine(jsonBook);
//------------Loop through the list-----------
foreach (Book book in books)
{
Console.WriteLine(book.Id);
Console.WriteLine(book.Title);
Console.WriteLine(book.Author);
Console.WriteLine(book.isbn);
Console.WriteLine();
}
Dictionary
//------------Dictionary List----------
Dictionary<string, string> person = new Dictionary<string, string>();
person.Add("name", "Md. Zahidul Islam");
person.Add("age", "30");
person.Add("mobile", "3434343");
person.Add("address","Dhaka");
string json = "{";
foreach (KeyValuePair<string,string> pair in person)
{
json+=pair.Key+":"+pair.Value+",";
}
json = json.TrimEnd(",");
json += "}";
Console.WriteLine(json);
Stack
//------Stack:LIFO----------//
Stack<Book> stack = new Stack<Book>();
stack.Push(new Book(){Id = 1, Title = "Introducing Java", Author = "Mr. Abc", isbn = "4343553"});
stack.Push(new Book(){Id = 2, Title = "Introducing C++", Author = "Mr. EFG", isbn = "34543543" });
Book b= stack.Pop();
Console.WriteLine(b.Id);
Console.WriteLine(b.Title);
Book b2 = stack.Pop();
Console.WriteLine(b2.Id);
Console.WriteLine(b2.Title);
Queue
//------Queue:FIFO----------//
Queue<Book> queue = new Queue<Book>();
queue.Enqueue(new Book() { Id = 1, Title = "Introducing Java", Author = "Mr. Abc", isbn = "4343553" });
queue.Enqueue(new Book() { Id = 2, Title = "Introducing C++", Author = "Mr. EFG", isbn = "34543543" });
Book b3=queue.Dequeue();
Console.WriteLine(b3.Id);
Console.WriteLine(b3.Title);
Book b4 = queue.Dequeue();
Console.WriteLine(b4.Id);
Console.WriteLine(b4.Title);
Table
//Table
DataTable table = new DataTable();
table.Columns.Add("Id",typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Roll", typeof(string));
table.Columns.Add("Score", typeof(int));
table.Rows.Add(1, "Md Zahidul Islam", "40", 90);
table.Rows.Add(2, "Md Abdur Rahman", "41", 92);
table.Rows.Add(3, "Rejaul Karim", "42", 82);
table.Rows.Add(4, "Mahfuzur Rahman", "46",70);
foreach(DataColumn col in table.Columns)
{
Console.Write(col.ColumnName+" | ");
}
Console.WriteLine();
foreach(DataRow row in table.Rows)
{
Console.WriteLine(row.Field<int>("Id")+" | "+row.Field<string>("Name")+" | "+row.Field<string>("Roll")+" | "+row.Field<int>("Score"));
}
Array List
Lecture:
1. The ArrayList class is designed to hold heterogeneous collections of objects. However, it does not always offer the best performance.
2. The ArrayList is not guaranteed to be sorted. ou must sort the ArrayList by calling its Sort method.
3. Using multidimensional arrays as elements in an ArrayList collection is not supported.
We don't recommend that you use the ArrayList class for new development. Instead, we recommend that you use the generic List<T> class.
ArrayList names = new ArrayList();
names.Add("Zahid");
names.Add("Tabina");
names.Add("Ali");
names.Sort();
foreach (var name in names)
{
Console.WriteLine(name);
}
Comments 1