Convert (Cast) between 2 custom classes

If you have ever wanted to convert (cast) between two custom classes, there is a simple way to do it,

public partial class xxxFinalClass
    {
        public static explicit operator xxxFinalClass(xxxOriginalClass data)
        {
            var temp = new xxxFinalClass();
            //do Conversion
            return temp;
        }
}

Now you can use like

xxxFinalClass newobj = (xxxFinalClass)oldobj;
19
Feb 2009
Author naresh
Category

.net Programming

Comments No Comments

Leave a Reply