C# – LinkedList

LinkedList

LinkedList<T> is a generic doubly linked list.

Doubly linked list is a chain of nodes in which each references the node before, the node after and the actual element.

Benefit – element can always be inserted efficiently anywhere in the list, since it just involves creating a new node and updating a few references.

Implements IEnumerable<T> and ICollection<T>.

LinkedList<T> supports copying to an array for indexed processing and obtaining an enumerator to support foreach statement.

public sealed class LinkedListNode
{
	public LinkedList List { get; }
	public LinkedListNode Next { get; }
	public LinkedListNode Previous { get; }
	public T Value { get; set; }
}
var tune = new LinkedList();
tune.AddFirst ("do");                           tune.Dump(); // do
tune.AddLast ("so");                            tune.Dump(); // do - so

tune.AddAfter (tune.First, "re");               tune.Dump(); // do - re- so
tune.AddAfter (tune.First.Next, "mi");          tune.Dump(); // do - re - mi- so
tune.AddBefore (tune.Last, "fa");               tune.Dump(); // do - re - mi - fa- so

tune.RemoveFirst();                             tune.Dump(); // re - mi - fa - so
tune.RemoveLast();                              tune.Dump(); // re - mi - fa

LinkedListNode miNode = tune.Find ("mi");
tune.Remove (miNode);                           tune.Dump(); // re - fa
tune.AddFirst (miNode);                         tune.Dump(); // mi- re - fa

C# – Encapsulation

Encapsulation

Is about creating a boundary around an object to separate its public and private implementation details in the class file.

Example

public class Contact
{
	private string fname;
	private string lname;
	private string emailaddr;
	private string phoneno;
	private string addr;
	private string cname;
	private byte[] photo;
	private DateTime dob;

	public string FirstName
	{
		get { return fname; }
		set { fname = value; }
	}

	public string LastName
	{
		get { return lname; }
		set { lname = value; }
	}

	public string EmailAddress
	{
		get { return emailaddr; }
		set 
		{
			if(emailaddr.Contains("@") && emailaddr.Contains("."))
			{
				emailaddr = value;
			}
			else 
			{
				throw new Exception("Invalid Email address!");
			}
		}
	}

	public string PhoneNo
	{
		get { return phoneno; }
		set { phoneno = value; }
	}

	public string Address
	{
		get { return addr; }
		set { addr = value; }
	}

	public string CompanyName
	{
		get { return cname; }
		set { cname = value; }
	}

	public byte[] Photo
	{
		get { return photo; }
		set { photo = value; }
	}

	public DateTime BirthDate
	{
		get { return dob; }
		set
		{
			if ((DateTime.Today.Year - value.Year) > 18)
			{
				dob = value;
			}
			else
			{
				throw new Exception("Invalid birth date. Age must be greater than 18.");
			}
	    }
    }
}

The Contact class stores contact information in private variables which are hidden from the external access. The access to the private variables can be granted through public properties.

Classes and Objects

Classes and Objects

Classes

Classes and objects are used everywhere in C# and .Net framework.

Class is a blueprint of creating a type which groups data and behavior of a type.

Class contains properties, methods and events.

Object

Is a instance of a class.

public class Employee
{
	...
}

Employee obj = new Employee();

Related Article
Class

C# – IReadOnlyList

IReadOnlyList<T>

Low version of IList<T> which provides only readonly view of list.

public interface IReadOnlyList<out T> : IEnumerable<T>, IEnumerable
{
	int Count { get; }
	T this[int index] { get; }
}

C# – IList

IList<T> and IList

IList<T>

Standard interface for collection indexable by position.

Functionality inherited from ICollection<T> and IEnumerable<T>.

Provides the ability to read or write an element by position.

public interface IList : ICollection, IEnumerable, IEnumerable
{
	T this [int index] { get; set; }
	int IndexOf (T item);
	void Insert (int index, T item);
	void RemoveAt (int index);
}

IndexOf – performs liner search, returns -1 if item not found.

Insert – inserts at specified index.

RemoveAt – removes at specified index.

IList

public interface IList : ICollection, IEnumerable
{
object this [int index] { get; set }
bool IsFixedSize { get; }
bool IsReadOnly { get; }
int Add (object value);
void Clear();
bool Contains (object value);
int IndexOf (object value);
void Insert (int index, object value);
void Remove (object value);
void RemoveAt (int index);
}

Add – returns an integer index of the newly added item.

C# – ICollection

ICollection<T> and ICollection

ICollection<T> is the standard interface for countable collections of objects.

It provides the ability to determine the size of a collection (Count).

If the collection is writable you can also Add, Remove and Clear items from the collection.

Collection can be traversed via foreach statement since it extends IEnumerable.

Methods and Properties

Contains – determine whether an item exists in the collection.

ToArray – copy the collection into an array.

IsReadOnly – determine whether the collection is read-only.

Count-size of the collection.

Add– add item to the collection.

Remove– remove item from collection.

Clear– clear collection.

Generic Collection

public interface ICollection<T> : IEnumerable<T>, IEnumerable
{
	int Count { get; }
	
	bool Contains (T item);
	void CopyTo (T[] array, int arrayIndex);
	bool IsReadOnly { get; }

	void Add(T item);
	bool Remove (T item);
	void Clear();
}

Non Generic Collection

public interface ICollection : IEnumerable
{
	int Count { get; }
	bool IsSynchronized { get; }
	object SyncRoot { get; }
	void CopyTo (Array array, int index);
}

The interface is implemented in conjunction with either IList or IDictionary.