implicit-cast

Which Json deserializer renders IList<T> collections?

蹲街弑〆低调 提交于 2020-01-12 04:06:12
问题 I'm trying to deserialize json to an object model where the collections are represented as IList<T> types. The actual deserializing is here: JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Deserialize<IList<Contact>>( (new StreamReader(General.GetEmbeddedFile("Contacts.json")).ReadToEnd())); Before i post the exception i'm getting you should know what the implicit conversions are. This is the Contact type: public class Contact { public int ID { get; set; }

C# implicit cast “overloading” and reflection problem

瘦欲@ 提交于 2020-01-11 09:51:17
问题 I've got a problem with the following code (which compiles but crashes): using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace ConsoleApplication1 { public struct MyBoolean { public bool Value { get; set; } //cast string -> MyBoolean public static implicit operator MyBoolean(System.String value) { return new MyBoolean() { Value = (value[0] == 'J') }; } //cast bool -> MyBoolean public static implicit operator MyBoolean(bool

C# enum to string auto-conversion?

孤街醉人 提交于 2020-01-03 07:14:08
问题 Is it possible to have the compiler automatically convert my Enum values to strings so I can avoid explicitly calling the ToString method every time. Here's an example of what I'd like to do: enum Rank { A, B, C } Rank myRank = Rank.A; string myString = Rank.A; // Error: Cannot implicitly convert type 'Rank' to 'string' string myString2 = Rank.A.ToString(); // OK: but is extra work 回答1: No. An enum is it's own type, if you want to convert it to something else, you have to do some work.

TypeScript: Implicit number to enum cast

自作多情 提交于 2019-12-29 01:34:15
问题 Why does the following compile in TypeScript? enum xEnum { X1,X2 } function test(x: xEnum) { } test(6); Shouldn't it throw an error? IMHO this implicit cast is wrong here, no? Here is the playground link. 回答1: This is part of the language specification (3.2.7 Enum Types): Enum types are assignable to the Number primitive type, and vice versa, but different enum types are not assignable to each other So the decision to allow implicit conversion between number and Enum and vice-versa is

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

删除回忆录丶 提交于 2019-12-25 04:02:27
问题 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

Implicit array casting in C#

坚强是说给别人听的谎言 提交于 2019-12-22 09:13:14
问题 I have the following classes with an implicit cast operator defined: class A { ... } class B { private A m_a; public B(A a) { this.m_a = a; } public static implicit operator B(A a) { return new B(a); } } Now, I can implicitly cast A to B. But why can't I implicitly cast A[] to B[] ? static void Main(string[] args) { // compiles A a = new A(); B b = a; // doesn't compile A[] arrA = new A[] {new A(), new A()}; B[] arrB = arrA; } Thanks, Malki. 回答1: As Mehrdad Afshari mentioned, you're out of

C++ rely on implicit conversion to bool in conditions?

拈花ヽ惹草 提交于 2019-12-19 18:56:39
问题 I found the following rule in a coding standards sheet : Do not rely on implicit conversion to bool in conditions. if (ptr) // wrong if (ptr != NULL) // ok How reasonable/usefull is this rule? How much overload on the compiled code? 回答1: In the strictest sense, you can rely on implicit conversions to bool. Backwards compatibility with C demands it. Thus it becomes a question of code readability. Often the purpose of code standards is to enforce a sameness to the code style, whether you agree

Java - why does char get implicitly cast to byte (and short) primitive, when it shouldn't?

£可爱£侵袭症+ 提交于 2019-12-17 16:35:57
问题 Certain functionality of the compiler puzzles me (Oracle JDK 1.7 using Eclipse). So I've got this book that says char primitive needs to be explicitly cast to short and byte and this all makes sense due the data types' allowed ranges don't overlap. In other words below code works (but wouldn't work without the explicit type casts): char c = '&'; byte b = (byte)c; short s = (short)c; Printing b or s correctly displays the number 38, which is the numeric equivalent of (&) in Unicode. Which

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

╄→尐↘猪︶ㄣ 提交于 2019-12-17 16:28:02
问题 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

What is the difference between static_cast and Implicit_cast?

一笑奈何 提交于 2019-12-17 15:46:22
问题 What is implicit_cast? when should I prefer implicit_cast rather than static_cast? 回答1: 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