casting

Assignment in an if statement

会有一股神秘感。 提交于 2019-12-29 02:18:28
问题 I have a class Animal , and its subclass Dog . I often find myself coding the following lines: if (animal is Dog) { Dog dog = animal as Dog; dog.Name; ... } For the variable Animal animal; . Is there some syntax that allows me to write something like: if (Dog dog = animal as Dog) { dog.Name; ... } 回答1: The answer below was written years ago and updated over time. As of C# 7, you can use pattern matching: if (animal is Dog dog) { // Use dog here } Note that dog is still in scope after the if

Changing type without changing bits

与世无争的帅哥 提交于 2019-12-29 01:51:09
问题 I want to take a stack variable and reinterpret cast it into an unsigned integer type of the same size in bytes. For example, I might want to take double value and cast it to an uint64_t , with the catch that the bits are not modified. And I want to do it in a generic fashion. If I was dealing with pointers, I would use a reinterpret_cast<uint64_t*>(double_ptr) . I have come up with a solution, which uses a dirty hack on reinterpret_cast , and is effective, but it requires quite a lot of meta

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

Why is List<Number> not a sub-type of List<Object>?

允我心安 提交于 2019-12-28 17:41:15
问题 public void wahey(List<Object> list) {} wahey(new LinkedList<Number>()); The call to the method will not type-check. I can't even cast the parameter as follows: wahey((List<Object>) new LinkedList<Number>()); From my research, I have gathered that the reason for not allowing this is type-safety. If we were allowed to do the above, then we could have the following: List<Double> ld; wahey(ld); Inside the method wahey, we could add some Strings to the input list (as the parameter maintains a

Java casting order

北战南征 提交于 2019-12-28 17:27:27
问题 Let's say I have the following setup class A { B foo(); } class C extends B { } // later A a = new A(); C theFoo = (C)a.foo(); We know a.foo() returns type B. When I do (C)a.foo() , is it Casting a to type C then attempting to call foo() on it? Calling foo() on a and casting the result to type C ? I'm finding it difficult to determine, and have always just played on the side of caution with extra parenthesis (which isn't a bad idea, for readability, but now I'm curious) This is in specific

Typecasting malloc C++ [duplicate]

不羁岁月 提交于 2019-12-28 13:56:14
问题 This question already has answers here : Do I cast the result of malloc? (26 answers) Closed 6 years ago . I have some C code with malloc statements in it that I want to merge with some C++ code. I was wondering when and why is typecasting a call to malloc neccessary in C++? For example: char *str = (char*)malloc(strlen(argv[1]) * sizeof(char)); 回答1: when and why is typecasting a call to malloc neccessary in C++? Always when not assigning to a void * , since void * doesn't convert implicitly

Casting an object to two interfaces at the same time, to call a generic method

房东的猫 提交于 2019-12-28 13:48:39
问题 I want to call a generic method that constrains the input type T to implement two interfaces: interface IA { } interface IB { } void foo<T>(T t) where T : IA, IB { } How can I fix the last line of void bar(object obj) { if (obj is IA && obj is IB) { foo((IA && IB)obj); } } ? Reflection probably allows to do the call, but I would like to stay within the language. 回答1: Does the C# 4.0 dynamic keyword get you out of jail (mostly) free? After all - you are already doing the type checking.

Casting an object to two interfaces at the same time, to call a generic method

十年热恋 提交于 2019-12-28 13:48:12
问题 I want to call a generic method that constrains the input type T to implement two interfaces: interface IA { } interface IB { } void foo<T>(T t) where T : IA, IB { } How can I fix the last line of void bar(object obj) { if (obj is IA && obj is IB) { foo((IA && IB)obj); } } ? Reflection probably allows to do the call, but I would like to stay within the language. 回答1: Does the C# 4.0 dynamic keyword get you out of jail (mostly) free? After all - you are already doing the type checking.

Is it possible to cast integer to enum? [duplicate]

强颜欢笑 提交于 2019-12-28 13:41:28
问题 This question already has answers here : How to cast int to enum? (27 answers) Closed last year . I got the following enum: public enum detallistaDocumentStatus { /// <remarks/> ORIGINAL, /// <remarks/> COPY, /// <remarks/> REEMPLAZA, /// <remarks/> DELETE, } then I got a class property of type detallistaDocumentStatus: public detallistaDocumentStatus documentStatus { get { return this.documentStatusField; } set { this.documentStatusField = value; } } In the real life the user will send us a

Is it possible to cast integer to enum? [duplicate]

泄露秘密 提交于 2019-12-28 13:41:02
问题 This question already has answers here : How to cast int to enum? (27 answers) Closed last year . I got the following enum: public enum detallistaDocumentStatus { /// <remarks/> ORIGINAL, /// <remarks/> COPY, /// <remarks/> REEMPLAZA, /// <remarks/> DELETE, } then I got a class property of type detallistaDocumentStatus: public detallistaDocumentStatus documentStatus { get { return this.documentStatusField; } set { this.documentStatusField = value; } } In the real life the user will send us a