using System.Diagnostics;
public static class StringExtension
public static string GetFileNameExtension(this string s)
int fileExtPos = s.LastIndexOf('.');
if (fileExtPos > s.LastIndexOf('\\'))
ext = s.Substring(fileExtPos, s.Length - fileExtPos);
if (ext.Length <= 1) ext = "";
public static string GetFileExtStringRef(ref string file) {
int fileExtPos = file.LastIndexOf('.');
if (fileExtPos > file.LastIndexOf('\\'))
return file.Substring(fileExtPos, file.Length - fileExtPos);
public static void Main()
string file = "cheese.log.txt";
Console.WriteLine("Inline String Manipulation");
Stopwatch s = new Stopwatch();
int fileExtPos = file.LastIndexOf('.');
if (fileExtPos > file.LastIndexOf('\\'))
ext = file.Substring(fileExtPos, file.Length - fileExtPos);
if (ext.Length <= 1) ext = "";
Console.WriteLine(ext+ " in ticks "+ s.ElapsedTicks);
Console.WriteLine("Native Path.IO Get Extension");
ext = Path.GetExtension(file);
Console.WriteLine(ext+ " in ticks "+ s.ElapsedTicks);
Console.WriteLine("String Extension call");
ext = file.GetFileNameExtension();
Console.WriteLine(ext+ " in ticks "+ s.ElapsedTicks);
Console.WriteLine("By Ref Method Call");
ext = GetFileExtStringRef(ref file);
Console.WriteLine(ext+ " in ticks "+ s.ElapsedTicks);