c# 4.0 - How to Implicitly Convert an Enumerable of a type with Implicit Conversion Operators in C# 4.0 -
given:
public struct id { readonly int m_id; public id(int id) { m_id = id; } public static implicit operator int(id id) { return id.m_id; } public static implicit operator id(int id) { return new id(id); } }
can implicitly convert
ienumerable<int>
to
ienumerable<id>
and vice versa. in way. note
var ids = new int[]{ 1, 2 }; ids.cast<id>();
does not appear work , covariance not appear working in case, either. of course, doing select work i.e.:
ids.select(id => new id(id));
but looking make work implicitly, writing:
ienumerable<id> ids = new int[]{ 1, 2 };
and yes, know can written as:
ienumerable<id> ids = new id[]{ 1, 2 };
but issue in cases enumerable of ints comes different source, such file example.
i sorry if there answer this, not find it.
according this answer want not possible. can close not implicitly casting id collection. :
public class ids : list<int> { public static implicit operator ids(int[] intarray) { var result = new ids(); result.addrange(intarray); return result; } }
then possible :
ids t = new [] { 3,4 };
Comments
Post a Comment