implicit-cast

Ambiguous call between overloads of two-way implicit castable types when a derived type of one is passed as parameter

依然范特西╮ 提交于 2019-11-28 07:48:04
问题 (Trying to find a title that sums up a problem can be a very daunting task!) I have the following classes with some overloaded methods that produce a call ambiguity compiler error: public class MyClass { public static void OverloadedMethod(MyClass l) { } public static void OverloadedMethod(MyCastableClass l) { } //Try commenting this out separately from the next implicit operator. //Comment out the resulting offending casts in Test() as well. public static implicit operator MyCastableClass

Is there a way to do dynamic implicit type casting in C#?

送分小仙女□ 提交于 2019-11-27 22:59:57
Given this class with an implicit cast operator: public class MyDateTime { public static implicit operator MyDateTime(System.Int64 encoded) { return new MyDateTime(encoded); } public MyDateTime(System.Int64 encoded) { _encoded = encoded; } System.Int64 _encoded; } I can now do the following: long a = 5; MyDateTime b = a; But NOT the following: long f = 5; object g = f; MyDateTime h = g; This gives a compile time: Cannot implicitly convert type 'object' to 'MyDateTime'. Makes sense to me. Now I modify the previous example as follows: long f = 5; object g = f; MyDateTime h = (MyDateTime)g; This

What is the difference between static_cast and Implicit_cast?

ε祈祈猫儿з 提交于 2019-11-27 19:51:24
What is implicit_cast? when should I prefer implicit_cast rather than static_cast? Johannes Schaub - litb I'm copying over from a comment i made to answer this comment at another place. You can down-cast with static_cast . Not so with implicit_cast . static_cast basically allows you to do any implicit conversion, and in addition the reverse of any implicit conversion (up to some limits. you can't downcast if there is a virtual base-class involved). But implicit_cast will only accept implicit conversions. no down-cast, no void*->T* , no U->T if T has only explicit constructors for U. Note that

No implicit int -> short conversion in ternary statement

一世执手 提交于 2019-11-27 09:32:31
short s; s = (EitherTrueOrFalse()) ? 0 : 1; This fails with: error CS0266: Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) Can anyone explain why this is so? The only thing I can think of is that the compiler doesn't look at the second value and doesn't know the range between the two, in the case I wrote something like short s; s = (EitherTrueOrFalse()) ? 0 : 65000; Correct? The only fix is with an ugly cast? Also, it seems C# does not have a type suffix for the short type. That's a pretty grave oversight IMO. Otherwise, that would've

How do I avoid implicit conversions on non-constructing functions?

不问归期 提交于 2019-11-27 07:58:18
How do I avoid implicit casting on non-constructing functions? I have a function that takes an integer as a parameter, but that function will also take characters, bools, and longs. I believe it does this by implicitly casting them. How can I avoid this so that the function only accepts parameters of a matching type, and will refuse to compile otherwise? There is a keyword "explicit" but it does not work on non-constructing functions. :\ what do I do? The following program compiles, although I'd like it not to: #include <cstdlib> //the function signature requires an int void function(int i);

Implicit casting of Null-Coalescing operator result

孤人 提交于 2019-11-27 07:48:08
问题 With the following understanding about null coalescing operator (??) in C#. int? input = -10; int result = input ?? 10;//Case - I //is same as: int result = input == null? input : 10; // Case - II While, by definition and usage, Case I and Case II are same. It is surprising to see that in Case-I compiler is able to implicitly cast int? to int while in Case-II it shows error: 'Error 1 Cannot implicitly convert type 'int?' to 'int'" What is it that I am missing about null-coalescing operator?

No implicit int -> short conversion in ternary statement

这一生的挚爱 提交于 2019-11-26 14:47:33
问题 short s; s = (EitherTrueOrFalse()) ? 0 : 1; This fails with: error CS0266: Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) Can anyone explain why this is so? The only thing I can think of is that the compiler doesn't look at the second value and doesn't know the range between the two, in the case I wrote something like short s; s = (EitherTrueOrFalse()) ? 0 : 65000; Correct? The only fix is with an ugly cast? Also, it seems C# does not

How do I avoid implicit conversions on non-constructing functions?

限于喜欢 提交于 2019-11-26 13:56:28
问题 How do I avoid implicit casting on non-constructing functions? I have a function that takes an integer as a parameter, but that function will also take characters, bools, and longs. I believe it does this by implicitly casting them. How can I avoid this so that the function only accepts parameters of a matching type, and will refuse to compile otherwise? There is a keyword "explicit" but it does not work on non-constructing functions. :\ what do I do? The following program compiles, although

Varying behavior for possible loss of precision

末鹿安然 提交于 2019-11-26 11:23:09
In Java, when you do int b = 0; b = b + 1.0; You get a possible loss of precision error. But why is it that if you do int b = 0; b += 1.0; There isn't any error? polygenelubricants That's because b += 1.0; is equivalent to b = (int) ((b) + (1.0)); . The narrowing primitive conversion (JLS 5.1.3) is hidden in the compound assignment operation. JLS 15.26.2 Compound Assignment Operators (JLS Third Edition): A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)) , where T is the type of E1 , except that E1 is evaluated only once. For example, the following

Varying behavior for possible loss of precision

巧了我就是萌 提交于 2019-11-26 02:23:33
问题 In Java, when you do int b = 0; b = b + 1.0; You get a possible loss of precision error. But why is it that if you do int b = 0; b += 1.0; There isn\'t any error? 回答1: That's because b += 1.0; is equivalent to b = (int) ((b) + (1.0)); . The narrowing primitive conversion (JLS 5.1.3) is hidden in the compound assignment operation. JLS 15.26.2 Compound Assignment Operators (JLS Third Edition): A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)) , where