Loading...
 
JoiWiki » Developer » .Net » .Net Theory » User Defined Conversions User Defined Conversions

User Defined Conversions

User Defined Conversions

User Defined Conversions are great, they allow you to to define ways that an object can be explicitly or implicitly cast as another object, which is lovely!.

 
Here's a quick code snippet in C# to show you what I mean, you can declare operators in this way to be explicit or implicit depending on how you want to be able to cast your objects around. I put this on a generic collection object that I'd defined so that standard lists of the same type could be easily cast:

 

/// <summary>
/// allows implicit conversion of a list of T to a DataClassList of T
/// </summary>
/// <param name="_Val"></param>
public static implicit operator DataClassList<T>(List<T> _Val)
{
	DataClassList<T> RetList = new DataClassList<T>();

	foreach (T item in _Val)
	{
		RetList.Add(item);
	}
	return RetList;
}


  

Although obviously there's probably a way that this could be set up to work better using lambda or similar 

 





 

 

 

 

 

 

 

 

Created by JBaker. Last Modification: Saturday May 11, 2019 20:28:12 BST by JBaker.

Developer