C#: Getting the dates from file names

Getting the dates from file names.

using System.Linq;
using System.Collections.Generic;
class Test
{
    static void Main()
    {
        var items = new List<string>() {
                "foo_20120212",
                "foo_20120213",
                "foo_20120214",
                "foo_20120215",
                "baz_20120212",
                "baz_20120213",
                "baz_20120214",
                "baz_20120215"};

        var v = items.Select(a => a.Insert(a.Length - 2, "/"))
                                .Select(b => b.Insert(b.Length - 5, "/"))
                                .Select(c => new { date = c.Split('_')[1], str = c.Replace("/", "") })
                                .Select(d => new { date = String.Concat(d.date.Split('/').Reverse()), str = d.str })
                                .Select(e => new { date = e.date.Insert(e.date.Length - 4, "/"), str = e.str })
                                .Select(f => new { date = f.date.Insert(f.date.Length - 7, "/"), str = f.str })
                                .Select(g => new { date = DateTime.Parse(g.date), str = g.str })
                                .OrderByDescending(h => h.date).ToList();

        v.ForEach(x => Console.WriteLine(String.Format("date: {0}, str: {1}", x.date, x.str)));
        Console.ReadKey();
    }
}

date

C#: Generic swap functions

This entry is part 70 of 71 in the series C#

Generic swap functions

namespace Test
{
	class Program
	{
		
		public static void Swap<T> (T a, T b)
		{
			var temp = a;
			a = b;
			b = temp;
		}

		public static void Swap<T> (ref T a, ref T b)
		{
			var temp = a;
			a = b;
			b = temp;
		}

		public static void SwapValue (SwapTest a, SwapTest b)
		{
			var temp = a.Value;
			a.Value = b.Value;
			b.Value = temp;
		}

		public class SwapTest
		{
			public string Value { get; set; }
		}

		static void Main ()
		{
			var a = new SwapTest { Value = "a" };
			var b = new SwapTest { Value = "b" };
			Swap (a, b);
			Console.WriteLine ("Value a: " + a.Value + " |  Value b: " + b.Value); 
			//output: Value a : a | Value b : b  

			a = new SwapTest { Value = "a" };
			b = new SwapTest { Value = "b" };
			Swap (ref a, ref b);
			Console.WriteLine ("Ref Value a: " + a.Value + " |  Ref Value b: " + b.Value); 
			//output: Ref Value a : b | Ref Value b : a 

			a = new SwapTest { Value = "a" };
			b = new SwapTest { Value = "b" };
			SwapValue (a, b);
			Console.WriteLine ("Swap Value a: " + a.Value + " |  Swap Value b: " + b.Value); 
			//output: Swap Value a : b | Swap Value b : a 
			Console.Read ();
		}
	}
}

swap