Mega Code Archive

 
Categories / C# Tutorial / Class
 

Extension method

using System;     public static class ExtensionMethods     {         public static string Right(this string s, int n)         {             if (n < 0 || n > s.Length)                 return s;             else                 return s.Substring(s.Length - n);         }     }     public class Tester     {         public static void Main()         {             string hello = "Hello";             Console.WriteLine("hello.Right(-1) = {0}", hello.Right(-1));             Console.WriteLine("hello.Right(0) = {0}", hello.Right(0));         }     }