boxing

Why does generic method with constraint of T: class result in boxing? [duplicate]

随声附和 提交于 2019-12-21 07:30:19
问题 This question already has answers here : Boxing when using generics in C# (2 answers) Closed 10 months ago . Why a generic method which constrains T to class would have boxing instructions in the generates MSIL code? I was quite surprised by this since surely since T is being constrained to a reference type the generated code should not need to perform any boxing. Here is the c# code: protected void SetRefProperty<T>(ref T propertyBackingField, T newValue) where T : class { bool isDifferent =

Box and UnBox what does it means? [duplicate]

☆樱花仙子☆ 提交于 2019-12-21 03:51:42
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: Why do we need boxing and unboxing in C#? What is boxing and unboxing and what are the trade offs? In C# what doe sit means: "Box and UnBox"? Here an extract from MSDN where I founded the Text. But this convenience comes at a cost. Any reference or value type that is added to an ArrayList is implicitly upcast to Object. If the items are value types, they must be boxed when they are added to the list, and

Do all C# casts result in boxing/unboxing

[亡魂溺海] 提交于 2019-12-20 18:34:57
问题 I am curious to know if all casts in C# result in boxing, and if not, are all casts a costly operation? Example taken from Boxing and Unboxing (C# Programming Guide) int i = 123; // The following line boxes i. object o = i; This line obviously causes boxing (wrapping up the int type as an object). This is an operation that is considered costly, since it creates garbage that will be collected. What about casts from 2 different types of reference types? what is the cost of that? can it be

Use cases for boxing a value type in C#?

拥有回忆 提交于 2019-12-20 12:34:28
问题 There are cases when an instance of a value type needs to be treated as an instance of a reference type. For situations like this, a value type instance can be converted into a reference type instance through a process called boxing. When a value type instance is boxed, storage is allocated on the heap and the instance's value is copied into that space. A reference to this storage is placed on the stack. The boxed value is an object, a reference type that contains the contents of the value

Boxing vs Unboxing

守給你的承諾、 提交于 2019-12-20 09:33:17
问题 Another recent C# interview question I had was if I knew what Boxing and Unboxing is. I explained that value types are on Stack and reference types on Heap. When a value is cast to a reference type, we call it boxing and vice versa. Then he asked me to calculate this: int i = 20; object j = i; j = 50; What is i ? I messed it up and said 50, where its actually 20. Now I think understand it why, however when I was playing with different combinations I was surprised to see this: Object a = 1; //

Why do some languages need Boxing and Unboxing?

馋奶兔 提交于 2019-12-20 08:53:28
问题 This is not a question of what is boxing and unboxing, it is rather why do languages like Java and C# need that ? I am greatly familiar wtih C++, STL and Boost. In C++ I could write something like this very easily, std::vector<double> dummy; I have some experience with Java, but I was really surprised because I had to write something like this, ArrayList<Double> dummy = new ArrayList<Double>(); My question, why should it be an Object, what is so hard technically to include primitive types

Extending java Integer cache

喜你入骨 提交于 2019-12-20 02:27:22
问题 There's a general advice to use Integer.valueOf(int) instead of new Integer(int) because of caching. In JDK 5+, you should really use valueOf because Integer now caches Integer objects between -128 and 127 and can hand you back the same exact Integer(0) object every time instead of wasting an object construction on a brand new identical Integer object. How can extend the range? 回答1: You can use the java.lang.Integer.IntegerCache.high property to increase the size of this cache. ex : java

Comparing structs for equality without boxing

北战南征 提交于 2019-12-19 17:34:34
问题 I came across an extension method that applies to structs (SomeStruct) and returns whether or not the value is equal to the default(SomeStruct) (when the parameterless constructor is called). public static bool IsDefault<T> (this T value) where T : struct { return (!EqualityComparer<T>.Default.Equals(value, default(T))); } This got me wondering whether the struct was being boxed. This is purely out of curiosity as there are pros/cons to boxing/passing by value depending on the context.

Is a C# struct ever boxed when declared as the return value of a function?

廉价感情. 提交于 2019-12-19 05:56:45
问题 A simple question, but I haven't found a definitive answer on Stack Overflow. struct MyStruct { int x, y, z; } MyStruct GetMyStruct() => new MyStruct(); static void Main() { var x = GetMyStruct(); // can boxing/unboxing ever occur? } Is a C# struct (value type) always copied to the stack when returned from a function, no matter how large it might be? The reason I'm unsure is that for some instruction sets other than MSIL (such as x86), a return value usually needs to fit into a processor

How can I convert a boxed two-dimensional array to a two-dimensional string array in one step?

亡梦爱人 提交于 2019-12-18 09:26:54
问题 Is there a way to convert a boxed two-dimensional array to a two-dimensional string array in one step using C#/.NET Framework 4.0? using ( MSExcel.Application app = MSExcel.Application.CreateApplication() ) { MSExcel.Workbook book1 = app.Workbooks.Open( this.txtOpen_FilePath.Text ); MSExcel.Worksheet sheet1 = (MSExcel.Worksheet)book1.Worksheets[1]; MSExcel.Range range = sheet1.GetRange( "A1", "F13" ); object value = range.Value; //the value is boxed two-dimensional array } I'm hopeful that