Club Manager : Extension Methods

Let us now create some helping extension methods. We are going to write Convert.ToInt32() a lot of time whenever we want to convert anything to int and similarly for Bool and so on. Let us create some extension methods for these in Core project which we can use again and again.

So add a new class library Core, delete class1.cs and add new class Extension and write extension methods for ToInt32, ToBoolean, ToDouble there.

We will add more as we require the same. Here goes the listing of Core.Extension.cs

using System;
 
namespace Core
{
    public static class Extension
    {
        public static Int32 ToInt32(this object obj)
        {
            return Convert.ToInt32(obj);
        }
 
        public static Boolean ToBoolean(this object obj)
        {
            return Convert.ToBoolean(obj);
        }
 
        public static Double ToDouble(this object obj)
        {
            return Convert.ToDouble(obj);
        }
    }
}

Leave a Reply