unboxing

Why does 'unbox.any' not provide a helpful exception text the way 'castclass' does?

眉间皱痕 提交于 2019-12-04 09:59:30
问题 To illustrate my question, consider these trivial examples (C#): object reference = new StringBuilder(); object box = 42; object unset = null; // CASE ONE: bad reference conversions (CIL instrcution 0x74 'castclass') try { string s = (string)reference; } catch (InvalidCastException ice) { Console.WriteLine(ice.Message); // Unable to cast object of type 'System.Text.StringBuilder' to type 'System.String'. } try { string s = (string)box; } catch (InvalidCastException ice) { Console.WriteLine

unboxing, (sparse) matrices, and haskell vector library

折月煮酒 提交于 2019-12-04 04:40:41
I would like to manipulate matrices (full or sparse) efficiently with haskell's vector library. Here is a matrix type import qualified Data.Vector.Unboxed as U import qualified Data.Vector as V data Link a = Full (V.Vector (U.Vector a)) | Sparse (V.Vector (U.Vector (Int,a))) type Vector a = U.Vector a As you can see, the matrix is a vector of unboxed vectors. Now, I would like to do a dot product between a vector and a matrix. It is fairly simple to do by combining a sum, zip and map. But if I do that, because I'm mapping through the rows of the matrix, the result is a boxed vector, even

Boxed Value Type comparisons

本秂侑毒 提交于 2019-12-04 00:55:18
What i'm trying to achieve here is a straight value comparison of boxed primitive types. ((object)12).Equals((object)12); // Type match will result in a value comparison, ((object)12).Equals((object)12d); // but a type mismatch will not. (false) object.Equals((object)12,(object)12d); // Same here. (false) I understand the 'why'. I just don't see a 'how'. The types are unknown until runtime, where they could be any primitive type from a datasource. That includes strings, datetimes, bools, etc. I've gone down the ugly route of writing an extension method that works out both types, and then casts

Boxing and unboxing when using out and ref parameters

别来无恙 提交于 2019-12-03 22:41:50
Does boxing/unboxing occur when a method accepts an out/ref parameter of a ValueType? For ref Keyword Its already mentioned on MSDN that : Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same. A method parameter can be modified by ref regardless of whether it is a value type or a reference type. There is no boxing of a value type when it is passed by reference. As for out keyword: The out keyword causes arguments to be passed by reference . This is like the ref keyword, except that ref requires that the variable be

Does unboxing occur when a class's value-type member is referenced?

一笑奈何 提交于 2019-12-03 14:22:24
I read What is boxing and unboxing and what are the trade offs? but can't understand one thing. Suppose I have a class: class MyClass { public int Value { get; set; } } And I want to get value within my method: void MyFunc(MyClass cls) { int i = cls.Value; } As a class placed in heap, I guess that Value placed in a heap too? And therefore operation int i = cls.Value; is unboxing? Or it's not unboxing? Stop thinking about stack and heap ; that's completely the wrong way to think about it. It is emphatically not the case that "boxed" means "on the heap", and therefore anything "on the heap" must

Box and UnBox what does it means? [duplicate]

你说的曾经没有我的故事 提交于 2019-12-03 12:32:18
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 unboxed when they are retrieved. Both the casting and the boxing and unboxing operations decrease performance; the effect of boxing and unboxing can be very significant in

How do I write a Data.Vector.Unboxed instance in Haskell?

百般思念 提交于 2019-12-03 08:51:19
问题 I've got a numeric application that does a lot of work with negative logs of probabilities, which (since probabilities range from zero to one) take the values of positive doubles, or negative infinity (if the underlying probability was zero). I'm using these with a newtype Score as follows: newtype Score = Score Double deriving (Eq, Ord) -- ^ A "score" is the negated logarithm of a probability negLogZero :: Score -- ^ Stands in for - log 0 negLogZero = Score 10e1024 negLogOne :: Score -- ^ -

Why does 'unbox.any' not provide a helpful exception text the way 'castclass' does?

不羁岁月 提交于 2019-12-03 04:34:26
To illustrate my question, consider these trivial examples (C#): object reference = new StringBuilder(); object box = 42; object unset = null; // CASE ONE: bad reference conversions (CIL instrcution 0x74 'castclass') try { string s = (string)reference; } catch (InvalidCastException ice) { Console.WriteLine(ice.Message); // Unable to cast object of type 'System.Text.StringBuilder' to type 'System.String'. } try { string s = (string)box; } catch (InvalidCastException ice) { Console.WriteLine(ice.Message); // Unable to cast object of type 'System.Int32' to type 'System.String'. } // CASE TWO: bad

How is it that an enum derives from System.Enum and is an integer at the same time?

时间秒杀一切 提交于 2019-12-03 02:16:33
问题 Edit : Comments at bottom. Also, this. Here's what's kind of confusing me. My understanding is that if I have an enum like this... enum Animal { Dog, Cat } ...what I've essentially done is defined a value type called Animal with two defined values, Dog and Cat . This type derives from the reference type System.Enum (something which value types can't normally do—at least not in C#—but which is permitted in this case), and has a facility for casting back and forth to/from int values. If the way

How do I write a Data.Vector.Unboxed instance in Haskell?

有些话、适合烂在心里 提交于 2019-12-02 22:44:27
I've got a numeric application that does a lot of work with negative logs of probabilities, which (since probabilities range from zero to one) take the values of positive doubles, or negative infinity (if the underlying probability was zero). I'm using these with a newtype Score as follows: newtype Score = Score Double deriving (Eq, Ord) -- ^ A "score" is the negated logarithm of a probability negLogZero :: Score -- ^ Stands in for - log 0 negLogZero = Score 10e1024 negLogOne :: Score -- ^ - log 1 negLogOne = Score 0.0 unScore :: Score -> Double unScore (Score x) = x instance Show Score where