Adding extension method for int : Extension « Class « C# / CSharp Tutorial






using System;
using System.Text;
using System.Reflection;

using System.Runtime.CompilerServices;

public static class MyExtensions
{
    public static void DisplayDefiningAssembly(this object obj)
    {
        Console.WriteLine(obj.GetType().Name);
        Console.WriteLine(Assembly.GetAssembly(obj.GetType()));
    }
    public static int ReverseDigits(this int i)
    {
        char[] digits = i.ToString().ToCharArray();
        Array.Reverse(digits);
        string newDigits = new string(digits);
        return int.Parse(newDigits);
    }
}
class Program
{
    static void Main(string[] args)
    {
        int myInt = 987;
        myInt.DisplayDefiningAssembly();
        Console.WriteLine("{0} is reversed to {1}", myInt, myInt.ReverseDigits());
        Console.ReadLine();
    }
}








7.60.Extension
7.60.1.Adding extension method for int
7.60.2.Adding extension to Stream
7.60.3.Adding reverse operation to string
7.60.4.Add title case to string class
7.60.5.Extension Method On Null Reference
7.60.6.extension method
7.60.7.conflicting extension methods
7.60.8.Extension Methods