.Net Framework
C# Tutorial
C# Control Statement
C# Arrays
C# Object Class
C# Properties
C# Inheritance
C# Polymorphism
C# Abstraction
C# Strings
C# Exception Handling
C# File IO
C# Collections
C# Generics
C# Delegates
C# Reflection
Anonymous Function
C# Multithreading
C# Synchronization
C# Web Service
C# Misc
C# New Features
C# Programs
C# Interview Questions
ADO.NET Tutorial
ASP.NET Tutorial
C# LinkedList
C# LinkedList class uses the concept of linked list. It allows us to insert and delete elements fastly. It can have duplicate elements. It is found in System.Collections.Generic namespace.
t allows us to add and remove element at before or last index.
C# LinkedList example
Let's see an example of generic LinkedList class that stores elements using AddLast() and AddFirst() methods and iterates elements using for-each loop.
using System;
using System.Collections.Generic;
public class LinkedListExample
{
public static void Main(string[] args)
{
// Create a list of strings
var names = new LinkedList();
names.AddLast("Sunil");
names.AddLast("Anil");
names.AddLast("Mathew");
names.AddLast("Pushkar");
names.AddFirst("Sunil");//added to first index
// Iterate list element using foreach loop
foreach (var name in names)
{
Console.WriteLine(name);
}
}
}
using System.Collections.Generic;
public class LinkedListExample
{
public static void Main(string[] args)
{
// Create a list of strings
var names = new LinkedList();
names.AddLast("Sunil");
names.AddLast("Anil");
names.AddLast("Mathew");
names.AddLast("Pushkar");
names.AddFirst("Sunil");//added to first index
// Iterate list element using foreach loop
foreach (var name in names)
{
Console.WriteLine(name);
}
}
}
Output:
Sunil
Anil
Mathew
Puskar
Anil
Mathew
Puskar
Note: Unlike List, you cannot create LinkedList using Collection initializer.