Tag: autoboxing

在这种情况下,C#generics是否会阻止结构的自动装箱?

通常,将结构S视为接口I将触发结构的自动装箱,如果经常这样做会对性能产生影响。 但是,如果我使用类型参数T : I I编写一个generics方法并用S调用它,那么编译器是否会省略装箱,因为它知道类型S并且不必使用接口? 这段代码显示了我的观点: interface I{ void foo(); } struct S : I { public void foo() { /* do something */ } } class Y { void doFoo(I i){ i.foo(); } void doFooGeneric(T t) where T : I { t.foo(); // <— Will an S be boxed here?? } public static void […]