string number = "10"; int castedNumber = ((IConvertible)number).ToInt32();
public interface IPerson { string Name { get; set; } int Age { get; set; } } public class Employee : IPerson { public string Name { get; set; } public int Age { get; set; } public string Department { get; set; } } public static IPerson CastToPerson(object obj) { return obj as IPerson; } public static Employee CastToEmployee(object obj) { return obj as Employee; }In this example, we define an interface ‘IPerson’ that has two properties, ‘Name’ and ‘Age’. We then define a class ‘Employee’ that implements the ‘IPerson’ interface, but also has an additional property ‘Department’. We then create two static methods: ‘CastToPerson’ and ‘CastToEmployee’. Both methods accept an object parameter and use the ‘as’ keyword (which is a shorthand for typecasting) to attempt to cast the object to the respective types. If the cast is successful, the method returns the casted object; otherwise, it returns null. Overall, IType Cast is a useful interface in C# for typecasting objects to specific types, and can be used in a variety of scenarios.