Questions tagged [boxing]

Boxing is when a value type is wrapped in a reference-type wrapper for the purposes of using it when polymorphism (conversion to Object or an interface) is required.

Filter by
Sorted by
Tagged with
1 vote
0 answers
69 views

In C#, Why Is A Stack of Structs Referencing Reference Types Slower to Push Than Structs Containing Value Types?

If I have a Stack, I would have expected that a mystack.Push() would be about the same performance for T being a struct, and maybe a little slower for T being a reference type. When I benchmark this ...
Alexander Flesher's user avatar
4 votes
4 answers
186 views

Integer -> double unboxes, but Double -> int doesn't unbox. Why?

To avoid confusion, I'm looking for the rule/JLS entry. I'm not asking why Double -> int would fail, I'm asking about the way it fails I'm aware of the lossy conversion, as I've mentioned in my ...
Vince's user avatar
  • 14.7k
5 votes
2 answers
141 views

How does boxing a trait affect lifetime of parameters passed to it ? (with a very specific example)

Here is a very simple but specific example that produces a compilation error I cannot understand: use std::path::Path; trait MyTrait<T> { fn my_func(&self, t: T); } struct MyImpl {} ...
fruphon's user avatar
  • 51
1 vote
1 answer
43 views

CIL shows `box` ops when the generic type is constrained to a class

I have the following method: public static bool EquivalentTo<T>(this T? current, T? compare) where T : class { if (current is null && compare is null) // both are null ...
IamIC's user avatar
  • 17.8k
2 votes
1 answer
93 views

How does Boxing move data from stack to heap?

I am new to Rust and trying to understand how boxing works. By default objects are allocated in Stack. In the follwing code the object "s" is allocated in the stack.This means the variables ...
Ashika Umanga Umagiliya's user avatar
0 votes
1 answer
82 views

How to reach the value of an object nested within an object?

I am somehow producing a C# object that contains another object structure. I am not sure how I can get hold of its content. I have an array whose value is {object[1,1]} and type object[,] If I ...
Douar Gwenn's user avatar
0 votes
2 answers
181 views

Why can't C# cast an object containing a boxed int to a double?

In C#, why does Example 1 work: int myValue1 = 11; double resultDirectlyFromInt = myValue1; But Example 2 does not: int myValue2 = 22; object myObject2 = myValue2; double resultFromBoxedInt = (double)...
Gilles jr Bisson's user avatar
3 votes
3 answers
115 views

Do strings get boxed in C#?

I understand that value types like int or bool in C# get boxed, like in this example: int i = 5; object o = i; // boxing occurs But do I need to worry about C# boxing string types as well? string e = ...
user3163495's user avatar
  • 2,675
3 votes
0 answers
75 views

Why doesn't IntStream.spliterator() return Spliterator.OfInt since Java 11?

In Java 8, IntStream.spliterator() is overridden from the base class to return type Spliterator.OfInt, which is a sub-interface providing methods that operate on an IntConsumer instead of a generic ...
Dan R's user avatar
  • 1,432
5 votes
1 answer
91 views

Why does invoking a method with a short choose the `void (int)` overload over `void (Short)`? [duplicate]

I am learning Java. When I call go(x), I get ‘int’. Why not ‘Short’? public class test { public static void go(Short n) {System.out.println("Short");} public static void go(int n) {...
kolinunlt's user avatar
  • 345
0 votes
1 answer
117 views

Does any built-in value type in C# implement any interface?

I'm studying Boxing in C#. And it says if a value type such as a struct implements an interface, there is a boxing operation when we assign an object of type struct to a variable of the type of the ...
HosseinSedghian's user avatar
3 votes
2 answers
160 views

In C#, is boxing and unboxing involved when casting long to ulong and vice versa?

I know that boxing then unboxing is computationally expensive, and it happens when casting a value type to object or other reference type then back to value type. However, does this happen when I'm ...
8749236's user avatar
  • 444
0 votes
1 answer
72 views

Array of integers boxing [duplicate]

Let's say I have an array of integers and attempt to do a call to CopyTo() onto a different array: int[] a = {1, 2, 3}; int[] b = new int[3]; a.CopyTo(b, 0); // does b have a completely new set of { ...
Visual Studio's user avatar
1 vote
1 answer
53 views

F# Testing for Base Types With Pattern Matching and Boxing of Tuples

I try to understand the concept of boxing and testing for base types, especially with tuples. I have two objects from an external C# library that have different concrete types but share a common base ...
7enderhead's user avatar
-4 votes
1 answer
122 views

Collect primitive array values into a collection of type Map

How can I convert an array of primitive int, long, or double values into a collection of type Map? import java.util.*; import java.util.function.*; import java.util.stream.*; public class ...
Mr. Polywhirl's user avatar
0 votes
1 answer
211 views

Is it possible to get a pointer to a boxed unmanaged value type?

Is it possible to get a pointer to a boxed unmanaged value type, without writing a large switch statement that does a cast for every supported type? Something like: object val; // Contains a ...
kaalus's user avatar
  • 4,475
-1 votes
1 answer
95 views

Is it boxing or unboxing?

I often get confused with boxing and unboxing. I mean I understand that both of them mean casting from System.Object to a value type which inherits from it (e.g. System.Int32 or System.Double) or ...
user avatar
0 votes
0 answers
116 views

How to merge detected boxes with opencv?

I wrote a code that can detect the differences between two pages, but I want the boxes close to each other to appear as a single box,I want to see it in the form of the purple box in the picture I ...
Ctnkaya's user avatar
  • 15
0 votes
1 answer
54 views

How to box VAR1 and unbox VAR2 according to the type of VAR1

Imagine having an integer var aaa = (int)1; -------> here I have an int var bbb = (object)aaa;-----> bbb is an object but the type is kept... var ccc = aaa.GetType();---> ... in fact here I ...
Patrick's user avatar
  • 3,125
9 votes
3 answers
378 views

Does having a wrapper object return value (e.g. Integer) cause auto boxing in Java?

I couldn't find a definitive answer for this seemingly simple question. If I write a method like this: public Integer getAnInt() { int[] i = {4}; return i[0]; } is the return value autoboxed into ...
L. Blanc's user avatar
  • 2,200
1 vote
1 answer
462 views

Cannot convert source type to target type (of known interface for T)?

I can't seem to compile the following code; I get the error: Cannot convert source type 'MyConcreteInterfaceThing' to target type 'T' How come? Here is the code: public interface IMyInterfaceThing { ...
Stefan Hendriks's user avatar
0 votes
0 answers
270 views

Create delegate for struct instance method without boxing the target

I want to create a delegate for a struct instance method without boxing in a performance critical section. Here is an example: var myStruct = new MyStruct(someData); Action<int> action = new ...
SENya's user avatar
  • 1,103
0 votes
0 answers
71 views

set value in C++/CX e.g. Box<Color>

When I was setting the background colour of the TitleBar, I used ref new Color( ) because it is a Color^, but Color has no constructors to set the red colour. So I'm trying to change the R Field value ...
羅小雪's user avatar
1 vote
0 answers
38 views

Boxing process of value type fields embedded in a reference type

I am currently learning c#. Trying to clarify some questions regarding boxing. Consider this example: using System; using System.IO; using Newtonsoft.Json; public sealed class Program { public ...
Gabib's user avatar
  • 11
2 votes
1 answer
365 views

Generic type parameters bound to value types - making them nullable

I have a generic class to represent either a result or an error, but not both: public class Result<TValue, TError> where TValue : notnull where TError : notnull { private readonly ...
Giles's user avatar
  • 1,353
-1 votes
1 answer
46 views

After boxing, cannot change the object value by passing the object as an argument of a function [duplicate]

I am boxing an integer as an object (System.Object). Then if I assign a new value to the object within the same scope, the value of the object is getting updated. But, if I try to pass the object as ...
Nafiz's user avatar
  • 43
1 vote
1 answer
274 views

Upcasting and down-casting is also boxing and unboxing?

I'm new to OOP and I have a question about these concept Do the upcasting and down-casting and boxing and unboxing have the same meaning is C#? Upcasting and down-casting is also boxing and unboxing?
hamed n's user avatar
1 vote
2 answers
641 views

Returning a closure from a method of a generic struct

I'm a newbie with Rust and I bumped into some obstacles when dealing with closures, either when returning them from functions and or methods, either when I need to store them as struct fields. Let's ...
se7entyse7en's user avatar
  • 4,370
5 votes
2 answers
235 views

Is it possible to force Clojure into using primitive types instead of their boxed versions?

I have a following sample of code: (type (apply / [5.0 0])) It throws an unexpected error - "Division By Zero" (expected behavior: return Inf) Probably, it happens due to auto-boxing - can ...
ischenko's user avatar
2 votes
1 answer
123 views

Reading and writing to boxed double values thread safe with no lock?

From the words of MS, reads and writes (along with other operations) to doubles are not atomic and thus not thread safe. I wanted to see if I can make reads and writes to double thread safe by boxing ...
Cabbage Champion's user avatar
1 vote
1 answer
202 views

How to get the integral value of an Enum member using Reflection?

Consider the following Enum: [Flags] public enum Digit: UInt64 { None = 00 << 00, One = 01 << 00, Two = 01 << 01, Three = 01 << 02, // Etcetera... ...
Raheel Khan's user avatar
  • 14.3k
2 votes
0 answers
103 views

Option type overhead for primitive types

Does the Scala Option type incur an overhead of two objects per primitive type value? In other words, is the Option type specialized for primitives, or does it always double-box them (I'm counting ...
Stephane Bersier's user avatar
6 votes
1 answer
506 views

Why does System.Int32 take 24 bytes?

If an int takes 4 bytes, why does System.Int32 takes 24 bytes when boxing an integer into an object? For example: int i = 3; object o = i;
ILovePizza's user avatar
0 votes
2 answers
430 views

Unity, C#, pointer workaround?

I have a field of type Color, let's call it objectColor. I want objectColor "point" to a gameobject that has a color field. For example, the background color of the camera. Or the color of a ...
zachwalker243's user avatar
1 vote
2 answers
322 views

Boxing type equality and dictionary keys

I have a bit of confusion about how dictionary compare keys when it comes to boxed types. using System; using System.Collections.Generic; public class Program { public static ...
Skary's user avatar
  • 1,332
0 votes
1 answer
98 views

Implement generic interface method in generic class

Is there any way to implement the interface ISetter so that I do not need boxing when assigning the value? A direct cast (T)value is obviously not possible (compiler error). Type T can be a value or a ...
LionAM's user avatar
  • 1,311
-1 votes
1 answer
445 views

Why does this code give a null pointer Exception? I thought Character class could handle null being assigned? [duplicate]

public class Playground { public static void main(String[] args) { String s = "blah"; Character lclfs = s.contains("/") ? '/' : s.contains("\\") ? '\\' : null; ...
JGFMK's user avatar
  • 8,521
1 vote
1 answer
297 views

How to remove boxing using Code Clean up in Intellij Idea?

There is a feature in Intellij Idea called Analyze > Code Cleanup. This cleans up the code (like removes unnecessary instances of this.). I am editing a very old script in a newer version of Java. ...
Day Trip's user avatar
1 vote
0 answers
58 views

How do you pass a Boxed trait object to a function? [duplicate]

I am trying to use rust (on Linux) to write simple commands to a serialport and return the response. I am using the serialport crate. I wanted to do reading in a separate function from Main. I ...
rasmus91's user avatar
  • 3,044
2 votes
1 answer
759 views

Is enum to int unboxing?

enum Season { spring, summer, fall, winter } int a = (int)Season.spring; Is this a unboxing or just a normal casting? If this is a unboxing, could you explain why? I thought this is just a normal ...
user avatar
0 votes
0 answers
27 views

Unboxing and Boxing in Java, but something feels strange [duplicate]

Integer a1=164; Integer a2=164; System.out.println(a1==a2); System.out.println(a1.equals(a2)); Integer a3=new Integer(164); Integer a4=new Integer(164)...
Chitransh Saxena's user avatar
-1 votes
1 answer
42 views

How to convert an arbitrary-dimension array of primitives into it's boxed counterpart

If I have an array such as int[][][] myArr = new int[][][] {{{1}, {1, 2, 3}}, {}}; how can I convert it it's boxed counterpart, i.e. Integer[][][] myArrBoxed = ...
Rubydesic's user avatar
  • 3,426
0 votes
1 answer
440 views

How to resolve boxing or conversion error for below example?

In the code below; CustomClass<EnType1> a = new CustomClass<EnType1>(); CustomClass<TEnum<EnType1>> b = new CustomClass<TEnum<EnType1>>(); Throws error The type ...
digitally_inspired's user avatar
0 votes
2 answers
837 views

The type 'int' cannot be used as type parameter 'T' in the generic method. There is no boxing conversion from 'int'

I'm trying to implement Sorting trash container. Unfortunately, I'm not so experienced in C# yet and I'm obviously not getting the point of handling the generic method and corresponding interfaces. ...
pcs7anwender's user avatar
0 votes
0 answers
36 views

I am attempting to use Reflection.FieldInfo.SetValue on a structure field to modify its value, but to no avail. Why? (Code in C# and VB provided.) [duplicate]

I'm attempting to use Reflection.FieldInfo.SetValue to modify a structure's integer field value. However, it doesn't get modified. I realize, that SetValue expects an Object, but boxing the integer ...
user avatar
2 votes
1 answer
165 views

Why Kotlin '===' reference equality operator returns false for same object references?

The example are from Kotlin official website val a: Int = 100 val boxedA: Int? = a val anotherBoxedA: Int? = a val b: Int = 100 val boxedB: Int? = b val anotherBoxedB: ...
Kushal's user avatar
  • 8,144
-1 votes
1 answer
235 views

Is each element of a List<T> boxed?

A boxed T can only be unboxed to T. For example, this is not working: object o1 = 5; double n3 = (double)o1; Using the Linq's Cast() method on a List<T> throws a similar exception: List<int&...
A.B.'s user avatar
  • 2,404
4 votes
2 answers
2k views

Creating closures in match arms

I'm going through the Rust Book and have been adapting the "minigrep" project such that instead of having both search and search_case_insensitive functions, I have a single search function ...
Xophmeister's user avatar
  • 8,924
0 votes
1 answer
27 views

getting an error in unboxing in c# stating name does not exist in current context?

class Program { static void Main(string[] args) { Console.WriteLine("enter id"); int id = Convert.ToInt32(Console.ReadLine()); Console....
shubham's user avatar
0 votes
0 answers
43 views

How to perform a reinterpretation cast in VB.NET

I'm working with an old Windows application made in vb.net and trying to open a file downloaded from my firmware but it requires a reinterpretation cast that I'm not sure how to do in vb.net In my C ...
CHollman82's user avatar

1
2 3 4 5
12