• .NET
  • C#
  • Enumerable
  • Tip of the Day

C# Tip of the Day 2020.01.15

Want a quick and dirty way to randomly order a list in C#, use the OrderBy like below.

using System;
using System.Linq;

public class Program
{
	public static void Main()
	{
		var neatList = Enumerable.Range(1, 100).Select(
			o => new ComplexObject
			{
				Name = $"I am complex object {o}. ;)"
			}
		);
		var dirtyList = neatList.OrderBy(_ => Guid.NewGuid());
		Console.WriteLine("First in 'neatList': {0}", neatList.First().Name);
		Console.WriteLine("First in 'dirtyList': {0}", dirtyList.First().Name);
	}
	public class ComplexObject
	{
		public string Name { get; set; }
	}
}
Cody's logo image, it is an abstract of a black hole with a white Event Horizon.

Cody Merritt Anhorn

A Engineer with a passion for Platform Architecture and Tool Development.