C# Extension Method: Object - ChangeType

CSharp · Console

/Program.cs

// C# Extension Method
// Doc: https://csharp-extension.com/en/method/1002556/object-changetype

// @nuget: Z.ExtensionMethods

using System;
					
public class Program
{
	public static void Main()
	{
        Double doubleVal = -2.345;
        string str = "12/12/98";

		//C# Extension Method: Object - ChangeType
        int i = (int)doubleVal.ChangeType(typeof(int));
        Console.WriteLine("The double value {0} when converted to an int becomes {1}", doubleVal, i);

		//C# Extension Method: Object - ChangeType
        DateTime dt = (DateTime)str.ChangeType(typeof(DateTime));
        Console.WriteLine("The string value {0} when converted to a Date becomes {1}", str, dt);
	}
}
;