explicit-conversion

why does explicit cast for generic list not work

拥有回忆 提交于 2021-02-20 00:48:38
问题 I am trying to cast a list of objects within a consturctor for a derive class IntersectionPath as follows. public class IntersectionPath : Path<IntersectionSegment>, IEnumerable { //Constructors public IntersectionPath() : base() { Verts = null; } public IntersectionPath(List<Intersection> inVerts, List<Segment<Node>> inEdges) : base() { this.Segments = (List<IntersectionSegment>) inEdges; } } Segments is defined in the generic base class Path public class Path<T> : IEnumerable<T> where T :

why does explicit cast for generic list not work

巧了我就是萌 提交于 2021-02-20 00:48:00
问题 I am trying to cast a list of objects within a consturctor for a derive class IntersectionPath as follows. public class IntersectionPath : Path<IntersectionSegment>, IEnumerable { //Constructors public IntersectionPath() : base() { Verts = null; } public IntersectionPath(List<Intersection> inVerts, List<Segment<Node>> inEdges) : base() { this.Segments = (List<IntersectionSegment>) inEdges; } } Segments is defined in the generic base class Path public class Path<T> : IEnumerable<T> where T :

why does explicit cast for generic list not work

半世苍凉 提交于 2021-02-20 00:44:33
问题 I am trying to cast a list of objects within a consturctor for a derive class IntersectionPath as follows. public class IntersectionPath : Path<IntersectionSegment>, IEnumerable { //Constructors public IntersectionPath() : base() { Verts = null; } public IntersectionPath(List<Intersection> inVerts, List<Segment<Node>> inEdges) : base() { this.Segments = (List<IntersectionSegment>) inEdges; } } Segments is defined in the generic base class Path public class Path<T> : IEnumerable<T> where T :

Why can't I implicitly convert a double to an int?

ε祈祈猫儿з 提交于 2020-07-03 07:02:17
问题 You can implicitly convert an int to a double: double x = 5; You can explicitly convert an int to a double: double x = (double) 5; You can explicitly convert a double to an int: int x = (int) 5.0; Why can't you implicitly convert a double to an int? : int x = 5.0; 回答1: The range of double is wider than int . That's why you need explicit cast. Because of the same reason you can't implicitly cast from long to int : long l = 234; int x = l; // error 回答2: Implicit casting is only available when

Why can't I implicitly convert a double to an int?

戏子无情 提交于 2020-07-03 07:02:03
问题 You can implicitly convert an int to a double: double x = 5; You can explicitly convert an int to a double: double x = (double) 5; You can explicitly convert a double to an int: int x = (int) 5.0; Why can't you implicitly convert a double to an int? : int x = 5.0; 回答1: The range of double is wider than int . That's why you need explicit cast. Because of the same reason you can't implicitly cast from long to int : long l = 234; int x = l; // error 回答2: Implicit casting is only available when

Explanation for C# Language Specification: 6.2.4 Explicit reference conversions

对着背影说爱祢 提交于 2020-01-15 04:20:28
问题 As I mentioned in this post, I faced a for me not understandable compiler behaviour. The code: IEnumerable<IList<MyClass>> myData = //...getMyData foreach (MyClass o in myData){} It compiles, but fails on runtime: InvalidCastException; for me it is obvious. If I change the IList to List as following, it complains: IEnumerable<List<MyClass>> myData = //...getMyData foreach (MyClass o in myData){} When instead of the class type I put var as following, intellisense recognizes the correct type:

Why/when is it important to specify an operator as explicit?

跟風遠走 提交于 2020-01-13 08:09:52
问题 I've borrowed the code below from another question (slightly modified), to use in my code: internal class PositiveDouble { private double _value; public PositiveDouble(double val) { if (val < 0) throw new ArgumentOutOfRangeException("Value needs to be positive"); _value = val; } // This conversion is safe, we can make it implicit public static implicit operator double(PositiveDouble d) { return d._value; } // This conversion is not always safe, so we're supposed to make it explicit public

How do I perform explicit operation casting from reflection?

安稳与你 提交于 2020-01-13 08:02:50
问题 I want to use reflection and do either an implicit or explicit coversion using reflection. Given I have defined Foo this way public class Foo { public static explicit operator decimal(Foo foo) { return foo.Value; } public static explicit operator Foo(decimal number) { return new Foo(number); } public Foo() { } public Foo(decimal number) { Value = number; } public decimal Value { get; set; } public override string ToString() { return Value.ToString(); } } When I run this code decimal

Difference between implicit conversion and explicit conversion [duplicate]

吃可爱长大的小学妹 提交于 2020-01-10 10:23:09
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Implicit VS Explicit Conversion What is the difference between "implicit conversion" and "explicit conversion"? Is the difference different in Java and C++? 回答1: An explicit conversion is where you use some syntax to tell the program to do a conversion. For example (in Java): int i = 999999999; byte b = (byte) i; // The type cast causes an explicit conversion b = i; // Compilation error!! No implicit conversion

Difference between implicit conversion and explicit conversion [duplicate]

房东的猫 提交于 2020-01-10 10:22:21
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Implicit VS Explicit Conversion What is the difference between "implicit conversion" and "explicit conversion"? Is the difference different in Java and C++? 回答1: An explicit conversion is where you use some syntax to tell the program to do a conversion. For example (in Java): int i = 999999999; byte b = (byte) i; // The type cast causes an explicit conversion b = i; // Compilation error!! No implicit conversion