-
Notifications
You must be signed in to change notification settings - Fork 1
Using ConvertTo
Lorenz Lo Sauer edited this page Oct 3, 2016
·
1 revision
The convert methods ConvertTo
and TryConvert
, can involve up to three different types, to convert an arbitrary input type to another arbitrary output type,
using an optional second model argument, which encapsulates all data required for the custom conversion function.
- Use Convert whenever an unrestricted Type A is to be changed into an unrestricted Type B, with an implied convertible relationship between the two and / or a complexity that requires additional arguments.
- Use TryConvert to prevent raising exceptions during the invocation of the custom conversion-logic.
Following is an example absent of accompanying notes:
Func<Point, Rectangle, Size> delegateThreeTypes = (ap, br) =>
{
var rect2 = (Rectangle)br;
if(ap.X * ap.Y > rect2.X * rect2.Y)
{
return new Size(ap.X, ap.Y);
}
return new Size(rect2.X, rect2.Y);
};
Point somePoint = new Point(1, 2);
Size size = somePoint.ConvertTo<Point, Size>(new Rectangle(1, 1, 2, 2));
Project Links
NuGet | MyGet| Appveyor | Twitter @sauerlo