867 questions
1
vote
1
answer
50
views
Doubly-boxed wildcard return type has an unexpected extra wildcard
I have a method that returns a doubly-boxed generic type. When that generic type is a wildcard, the return type is not the one that I expect.
An object of type Codec<T> can produce a Box<Box&...
1
vote
1
answer
88
views
C# 8 - using both nullable and non-nullable version of the type in the generic
I need to use two versions of base type in the same generic: nullable and non-nullable.
Like int? and int, Guid? and Guid, DateOnly? and DateOnly and so on.
Nullable type should be at the class ...
4
votes
1
answer
116
views
Why does calling an unoverridden struct method require boxing?
According to boxing on structs when calling ToString(), calling an unoverridden method on a C# struct causes it to be boxed. This is ultimately because of how the constrained callvirt IL instruction ...
2
votes
0
answers
34
views
Does a default interface implementation on a struct introduce boxing? [duplicate]
I am making a simple network messaging system, and I have designed it around using structs for any small messages (and I am trying to avoid boxing as much as possible). The system is already working, ...
3
votes
1
answer
81
views
In Kotlin kotest, boxing of number giving false for number less than 128
The Kotlin Docs contains the following information.
All nullable references to a are actually the same object because of the memory optimization that JVM applies to Integers between -128 and 127. It ...
0
votes
1
answer
116
views
Boxing allocation in Composite Formatting
In Rider Resharper suggests to simplify string interpolation.
When doing so, the Heap Allocation Viewer plugin warns me about boxing allocation. And indeed, the IL code is different and when running ...
0
votes
1
answer
84
views
Using an interface implemented by a struct to not depend on concretions but to depend on abstractions (SOLID)
I am trying to follow the SOLID principles in my new C# 12/.NET 8 project.
I will start by providing an example:
Given two separate assemblies AssemblyA and AssemblyB where AssemblyA defines an ...
4
votes
4
answers
286
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 ...
1
vote
1
answer
63
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
...
0
votes
1
answer
98
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 ...
2
votes
3
answers
232
views
Do strings get boxed in C#? [duplicate]
Update Jan 5, 2024: the answer at this link appears to show that the C# compiler completely removes the object cast when trying to cast a string to an object: https://stackoverflow.com/a/51381643/...
5
votes
1
answer
109
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) {...
3
votes
2
answers
356
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 ...
1
vote
1
answer
71
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 ...
0
votes
1
answer
492
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 ...
-1
votes
1
answer
100
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 ...
0
votes
0
answers
148
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 ...
9
votes
3
answers
516
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 ...
1
vote
1
answer
1k
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
{
...
0
votes
0
answers
84
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 ...
0
votes
2
answers
117
views
Quick ValueTuple initialization with 1 argument
I want to create a simple way to create a matrix-like structure for users. This means that the user can define rows and columns.
For now its looks like this:
var matrix = new()
{
new() { Item1, ...
-1
votes
1
answer
68
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 ...
1
vote
2
answers
963
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 ...
0
votes
0
answers
2k
views
How to check if a parameter exists or not?
So I have a video inventory program,
Where the user creates a object of the class video by initializing the constructor
Video video=new Video(String name){
this.name=name
}
Now I have three more ...