casting

How do I convert IEnumerable to a custom type in C#?

狂风中的少年 提交于 2019-12-21 16:21:46
问题 I am using extension methods OrderBy and ThenBy to sort my custom collection on multiple fields. This sort does not effect the collection but instead returns and IEnumberable. I am unable to cast the IEnumerable result to my custom collection. Is there anyway to change the order of my collection or convert the IEnumerable result to my custom collection? 回答1: If your collection type implements IList<T> (to be able to Add() to it) you could write an extension method: public static Extensions {

Why do I need to cast self to id?

丶灬走出姿态 提交于 2019-12-21 14:37:03
问题 I have an init method that takes an (id) argument: -(id) initWithObject:(id) obj; I'm trying to call it like this: [[MyClass alloc] initWithObject:self]; But XCode is complaining about the argument being a "distinct Objective-C type" (which usually indicates a type mismatch or level of indirection error). If I explicitly cast self to (id) the warning goes away. In either case the code runs as expected. Interestingly, on the next line I'm passing self to another method that also takes an id,

Different behavior among explicit cast, direct initialization and copy initialization

大兔子大兔子 提交于 2019-12-21 12:15:03
问题 I have a class C which has a casting operator to anything. In the example I tried to cast an instance of it to std::string in three different ways: static_cast , constructor of std::string and assigning to std::string . However, only the last one compiles, while the others raise an error of ambiguous constructor. The reason of the error is clear enough: there are many ways to convert C to something which the constructor of std::string can accept. But what is the difference between these cases

Does multiplying unsigned short cause undefined behaviour?

限于喜欢 提交于 2019-12-21 10:46:36
问题 As a follow-up to "https://stackoverflow.com/questions/33732041/why-static-castunsigned-intushrt-maxushrt-max-yields-correct-value" I was asking myself if promoting all types (except some exceptions) with a lower rank than int to int to perform arithmetic operations might cause UB in some cases. e.g.: unsigned short a = 0xFFFF; unsigned short b = a*a; As unsigned short is promoted to int for arithmetic operations this would result in: unsigned short a = 0xFFFF; unsigned short b = (int)a*(int

Why is “operator void” not invoked with cast syntax?

和自甴很熟 提交于 2019-12-21 09:34:15
问题 While playing with this answer by user GMan I crafted the following snippet (compiled with Visual C++ 9): class Class { public: operator void() {} }; Class object; static_cast<void>( object ); (void)object; object.operator void(); after stepping over with the debugger I found out that casting to void doesn't invoke Class::operator void() , only the third invokation (with explicitly invoking the operator) actually invokes the operator, the two casts just do nothing. Why is the operator void

Coverting List of Dictionary to DataTable

橙三吉。 提交于 2019-12-21 09:33:48
问题 Currently we are doing this by looping through each value of list and dictionary: private DataTable ChangeToDictionary(List<Dictionary<string,int>> list) { DataTable datatTableReturn = new DataTable(); if (list.Count() > 0) { Dictionary<string, int> haeders = list.ElementAt(0); foreach (var colHead in haeders) { datatTableReturn.Columns.Add(colHead.Key); } } foreach (var row in list) { DataRow dataRow = datatTableReturn.NewRow(); foreach (var col in row) { dataRow[col.Key] = col.Value; }

Should I really use static_cast every single time I want to convert between primitive types?

痞子三分冷 提交于 2019-12-21 09:16:59
问题 What makes this long l = 1; char c = static_cast<char>(l); float f = 1.0f; int i = static_cast<int>(f); better than this long l = 1; char c = (char)l; float f = 1.0f; int i = (int)f; when casting one primitive data type to another? I've got much of legacy code that uses the second style for type casting in similar situations, so this is also a question about should I or may I not perform full-scale revision of that code. 回答1: Future-proofing. Let's say in the future I do this: float blah = 1

Should I really use static_cast every single time I want to convert between primitive types?

不羁的心 提交于 2019-12-21 09:16:22
问题 What makes this long l = 1; char c = static_cast<char>(l); float f = 1.0f; int i = static_cast<int>(f); better than this long l = 1; char c = (char)l; float f = 1.0f; int i = (int)f; when casting one primitive data type to another? I've got much of legacy code that uses the second style for type casting in similar situations, so this is also a question about should I or may I not perform full-scale revision of that code. 回答1: Future-proofing. Let's say in the future I do this: float blah = 1

Is const_cast<const Type*> ever useful?

岁酱吖の 提交于 2019-12-21 08:27:05
问题 Recently I found a piece of C++ code that effectively does the following: char* pointer = ...; const char* constPointer = const_cast<const char*>( pointer ); Obviously the author thought that const_cast means "add const", but in fact const can be just as well added implicitly: const char* constPointer = pointer; Is there any case when I would really have to const_cast to a pointer-to-const ( const_cast<const Type*> as in above example)? 回答1: const_cast , despite its name, is not specific to

F# Unit of Measure, Casting without losing the measure type

眉间皱痕 提交于 2019-12-21 07:55:11
问题 Is there built in version of the type casting functions that preserves units and if not how would I make them? So for example with this code how would I cast intWithSecondsMeasure to a float without losing the measure or multiplying by 1.0<s> ? [<Measure>] type s let intWithSecondsMeasure = 1<s> let justAFloat = float intWithSecondsMeasure 回答1: The answer provided by @kvb certainly works, but I'd prefer not to use the unbox operator for this conversion. There's a better, built in way that I