Add Element To The Beginning Of The List, In C# - Programming Idioms

This language bar is your friend. Select your favorite languages! Select your favorite languages :
  • C
  • C++
  • C#
  • Go
  • Java
  • JS
  • Obj-C
  • PHP
  • Python
  • Ruby
  • Rust
  • Or search :
Idiom #224 Add element to the beginning of the list

Insert the element x at the beginning of the list items.

  • C#
  • C#
  • Clojure
  • C++
  • C++
  • D
  • Dart
  • Elixir
  • Fortran
  • Go
  • Go
  • Go
  • Go
  • Haskell
  • Haskell
  • JS
  • JS
  • Java
  • Lisp
  • Lua
  • PHP
  • Pascal
  • Perl
  • Python
  • Python
  • Ruby
  • Ruby
  • Rust
  • Scala
  • VB
  • VB
  • C#
items.Insert(0, x);
  • Doc
  • C#
using System.Linq; items = items.Prepend(x).ToList();
  • Doc
  • Clojure
  • C++
  • C++
  • D
  • Dart
  • Elixir
  • Fortran
  • Go
  • Go
  • Go
  • Go
  • Haskell
  • Haskell
  • JS
  • JS
  • Java
  • Lisp
  • Lua
  • PHP
  • Pascal
  • Perl
  • Python
  • Python
  • Ruby
  • Ruby
  • Rust
  • Scala
  • VB
  • VB
(def items2 (conj items x))
  • Demo
  • Doc
#include <list> items.emplace_front(x);
  • Doc
#include <list> items.push_front(x);
  • Doc
items = x ~ items;
  • Demo
  • Doc
items = [x, ...items];
  • Doc
items2 = [x | items] items = [x, items] items = append([]T{x}, items...)
  • Demo
func prepend[S ~[]T, T any](items *S, x ...T) { *items = append(*items, x...) copy((*items)[len(x):], *items) copy(*items, x) }
  • Demo
func prepend[S ~[]T, T any](items *S, x ...T) { *items = append(x, *items...) }
  • Demo
items = append(items, x) copy(items[1:], items) items[0] = x
  • Demo
f(x:xs)= x:xs items2 = x : items items = [x, ...items]; items.unshift(x);
  • Doc
items.add(0, x);
  • Doc
(push x items)
  • Doc
table.insert(items, 1, x) array_unshift($items, $x);
  • Demo
  • Doc
items.insert(0,x); unshift @items, $x
  • Doc
items.insert(0, x)
  • Doc
items = [x] + items items.prepend(x)
  • Doc
items.unshift(x)
  • Doc
use std::collections::VecDeque; items.push_front(x);
  • Demo
  • Doc
val newList = x :: items Imports System.Linq items = items.Prepend(x).ToList()
  • Doc
items.Insert(0, x)
  • Doc
Do you know the best way to do this in your language ? New implementation... < > ⌨ Idiom created by programming-idioms.org History
  • View revisions
Related idioms
  • Add an element at the end of a list
  • Concatenate two lists
Cheatsheets Issues
  • Report a bug
×

Please choose a nickname before doing this

No security, no password. Other people might choose the same nickname.

OK
  • C#
items.Insert(0, x);
  • C#
items = items.Prepend(x).ToList();
  • Clojure
(def items2 (conj items x))
  • C++
items.emplace_front(x);
  • C++
items.push_front(x);
  • D
items = x ~ items;
  • Dart
items = [x, ...items];
  • Elixir
items2 = [x | items]
  • Fortran
items = [x, items]
  • Go
items = append([]T{x}, items...)
  • Go
func prepend[S ~[]T, T any](items *S, x ...T) { *items = append(*items, x...) copy((*items)[len(x):], *items) copy(*items, x) }
  • Go
func prepend[S ~[]T, T any](items *S, x ...T) { *items = append(x, *items...) }
  • Go
items = append(items, x) copy(items[1:], items) items[0] = x
  • Haskell
f(x:xs)= x:xs
  • Haskell
items2 = x : items
  • JS
items = [x, ...items];
  • JS
items.unshift(x);
  • Java
items.add(0, x);
  • Lisp
(push x items)
  • Lua
table.insert(items, 1, x)
  • PHP
array_unshift($items, $x);
  • Pascal
items.insert(0,x);
  • Perl
unshift @items, $x
  • Python
items.insert(0, x)
  • Python
items = [x] + items
  • Ruby
items.prepend(x)
  • Ruby
items.unshift(x)
  • Rust
items.push_front(x);
  • Scala
val newList = x :: items
  • VB
items = items.Prepend(x).ToList()
  • VB
items.Insert(0, x)

Tag » Add Element Ienumerable C#