c# - Initialize object by assignment? -


how give types ability initialize via assignment, following:

public struct wrappedbyte {     private byte m_value; }  //usage:     wrappedbyte x = 0xff; 

you need use custom implicit operator. note doesn't apply structs.

public struct wrappedbyte {     private byte m_value;      public static implicit operator wrappedbyte(byte b)     {         return new wrappedbyte() { m_value = b };     } } 

also note won't apply initialization; mean can supply byte in location wrappedbyte expected. includes assignments other initializations, parameters methods, etc.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -