When to use Cast() and Oftype() in Linq

I am aware of two methods of casting types to IEnumerable from an Arraylist in Linq and wondering in which cases to use them? eg IEnumerable<string> someCollection = arrayList.OfType<string>() or IEnumerable<string> someCollection = arrayList.Cast<string>() What is the difference between these two methods and where should I apply each case? OfType - return only the

何时在Linq中使用Cast()和Oftype()

我知道两种从Linq的Arraylist中将类型转换为IEnumerable方法,并想知道在哪些情况下使用它们? 例如 IEnumerable<string> someCollection = arrayList.OfType<string>() 要么 IEnumerable<string> someCollection = arrayList.Cast<string>() 这两种方法有什么区别,我应该在哪里应用每种情况? OfType - 仅返回类型x的元素。 Cast - 将尝试将所有元素转换为x类型。 如果其中一些不属于这种

Casting: (NewType) vs. Object as NewType

Possible Duplicate: Casting vs using the 'as' keyword in the CLR What is actually the difference between these two casts? SomeClass sc = (SomeClass)SomeObject; SomeClass sc2 = SomeObject as SomeClass; Normally, they should both be explicit casts to the specified type? The former will throw an exception if the source type can't be cast to the target type. The latter will resul

Casting:(NewType)与Object作为NewType

可能重复: 投射vs在CLR中使用'as'关键字 这两个演员之间究竟有什么区别? SomeClass sc = (SomeClass)SomeObject; SomeClass sc2 = SomeObject as SomeClass; 通常情况下,他们都应该显式转换为指定的类型? 如果源类型不能转换为目标类型,前者将抛出异常。 后者将导致sc2为空引用,但也不例外。 [编辑] 我的原始答案肯定是最明显的差异,但正如Eric Lippert指出的那样,它不是唯一的答案。 其他差异包

Casting vs using the 'as' keyword in the CLR

When programming interfaces, I've found I'm doing a lot of casting or object type conversion. Is there a difference between these two methods of conversion? If so, is there a cost difference or how does this affect my program? public interface IMyInterface { void AMethod(); } public class MyClass : IMyInterface { public void AMethod() { //Do work } // Othe

投射vs在CLR中使用'as'关键字

在编程接口时,我发现我正在进行大量的转换或对象类型转换。 这两种转换方法有区别吗? 如果是这样,是否存在成本差异,或者这对我的计划有什么影响? public interface IMyInterface { void AMethod(); } public class MyClass : IMyInterface { public void AMethod() { //Do work } // Other helper methods.... } public class Implementation { IMyInterface _MyObj; MyClass _my

WPF: Combobox losing selected index after bound ItemSource collection changes

I've searched Google and here for answers, my problem is somewhat related to the below question, but different enough to warrant a new question. Combo-box loses selection after collection changes Basically, I have a WPF combobox which is bound to a ObservableCollection class. This class has extra functionality to delay collection change notifications if I need to do a number of alteratio

WPF:绑定的ItemSource集合发生更改后,组合框丢失选定的索引

我搜索了谷歌并在这里寻找答案,我的问题与下面的问题有一定的关系,但不同的地方足以提出一个新的问题。 收集更改后,组合框会丢失选择 基本上,我有一个绑定到ObservableCollection类的WPF组合框。 如果需要对数据库进行一系列更改(如清理并重新填充数据库以获取全新数据库快照),则此类具有延迟收集更改通知的额外功能。 我的组合框绑定同时具有DisplayMemberPath和SelectedValuePath集。 SelectedValuePath解析为

C: macro produced warning "statement with no effect"

I have following code in C: int do_something(void); #ifdef SOMETHING #define DO_SOMETHING() do_something() #else #define DO_SOMETHING() 0 #endif This code produced warning "statement with no effect" when compiled without SOMETHING defined. I am trying to fix it, but there is one problem - code which uses this macro sometimes checks that "return value" and sometimes ignores

C:宏产生警告“声明无效”

我在C中有以下代码: int do_something(void); #ifdef SOMETHING #define DO_SOMETHING() do_something() #else #define DO_SOMETHING() 0 #endif 此代码在没有定义SOMETHING的情况下编译时会产生警告“无效”语句。 我试图修复它,但有一个问题 - 使用此宏的代码有时会检查“返回值”,有时会忽略它。 正因为如此,我无法使用最简单的解决方案 - 在宏本身中强制失效。 是否可以编写宏来比较“返回值”,并在忽略时不会产生此警

What do these strange macro definitions mean (and are they even correct?)

I am working on some legacy C code and have come accross two strange macro definitions. They don't look right, and are also responsible for some compiler warnings ( warning: left-hand operand of comma expression has no effect ), which took me several hours to finally track down to these macros. Can anyone tell me if they are correct (I suspect not), and if not, how do I fix them? #define

这些奇怪的宏定义是什么意思(它们甚至是正确的?)

我正在研究一些遗留的C代码,并且讨论了两个奇怪的宏定义。 它们看起来不正确,并且还对一些编译器警告负责( 警告:逗号表达式的左侧操作数不起作用 ),这花了我几个小时才终于追踪到这些宏。 任何人都可以告诉我他们是否正确(我怀疑不是),如果不是,我该如何解决? #define MAX_MEMORY_BLOCK (sizeof(size_t)==2,65535,2147483647) #define MAX_ARRAY_SIZE (sizeof(size_t)==2,16384,1073741824) 它们包含逗号运算

"unused parameter" warnings in C

What's the best way to suppress unused parameter warning in C code. For instance, Bool NullFunc(const struct timespec *when, const char *who) { return TRUE; } In C++ I was able to put a /*...*/ comment around the parameters. But not in C of course. It gives me error: parameter name omitted . I usually write a macro like this: #define UNUSED(x) (void)(x) You can use this macro fo

C中的“未使用的参数”警告

在C代码中抑制未使用的参数警告的最佳方法是什么? 例如, Bool NullFunc(const struct timespec *when, const char *who) { return TRUE; } 在C ++中,我能够在参数中加入/*...*/注释。 但当然不是。 它给了我error: parameter name omitted 。 我通常写这样一个宏: #define UNUSED(x) (void)(x) 你可以使用这个宏来处理所有未使用的参数。 (请注意,这适用于任何编译器。) 例如: void f(int x) { UNU

undefined reference to `pthread

I'm creating new threads in a function, and I've included pthread.h. But it's not working, I keep receiving the following error upon compiling: undefined reference to `pthread_create' The flags I'm using to compile are the following: CFLAGS=-std=gnu99 -pthread -g -Wall -Wextra -Werror -Wmissing-declarations -Wmissing-prototypes -Werror-implicit-function-declaration -Wre

未定义的参考`pthread

我在函数中创建新的线程,并且包含了pthread.h。 但它不起作用,编译时我一直收到以下错误: 对`pthread_create'的未定义引用 我用来编译的标志如下: CFLAGS = -std = gnu99 -pthread -g -Wall -Wextra -Werror -Wmissing-declarations -Wmissing-prototypes -Werror-implicit-function-declaration -Wreturn-type -Wparentheses -Wunused -Wold-style-definition -Wundef -Wshadow -Wswitch-default -Wunreachable-

How does this code print 404?

I copied this below code from Stack Overflow's 404 Not Found Error Page . # define v putchar # define print(x) main(){v(4+v(v(52)-4));return 0;}/* #>+++++++4+[>++++++<-]> ++++.----.++++.*/ print(202*2);exit(); #define/*>.@*/exit() The above code compiles fine and prints 404 on the console. I thought the statement print(202*2); is responsible for printing 404 , but I am not

这段代码如何打印404?

我从Stack Overflow的404 Not Found Error Page中复制了以下代码。 # define v putchar # define print(x) main(){v(4+v(v(52)-4));return 0;}/* #>+++++++4+[>++++++<-]> ++++.----.++++.*/ print(202*2);exit(); #define/*>.@*/exit() 上面的代码编译得很好,并在控制台上打印404 。 我认为这个声明打印(202 * 2); 是负责打印404 ,但我不是正确的,因为更改此声明中的数字也打印404 。 有人能帮我理解

GCC C compiler warning "warning: control reaches..."

This question already has an answer here: What should main() return in C and C++? 19 answers Your main function doesn't explicitly return anything. Just slap a return 0; at the end and you should be OK: int main() { char s1[] = "abcd"; char s2[] = "zzzz"; contract(s1,s2); return 0; /* Here! */ } The -ansi option switches to the ISO C90 standard. Back then, the main f

GCC C编译器警告“警告:控制到达...”

这个问题在这里已经有了答案: main()应该在C和C ++中返回什么? 19个答案 你的主函数不会显式地返回任何东西。 只是一个return 0; 最后,你应该可以: int main() { char s1[] = "abcd"; char s2[] = "zzzz"; contract(s1,s2); return 0; /* Here! */ } -ansi选项切换到ISO C90标准。 那时, main函数必须返回一些东西。 自C99以来,可以省略return语句。 当使用-ansi (= C89)时,如果你不返回