boxing

boxing unboxing

夙愿已清 提交于 2019-12-02 15:21:25
问题 I found the following code snippet while searching about boxing and unboxing in C#. class TestBoxing { static void Main() { int i = 123; // Boxing copies the value of i into object o. object o = i; // Change the value of i. i = 456; // The change in i does not effect the value stored in o. System.Console.WriteLine("The value-type value = {0}", i); System.Console.WriteLine("The object-type value = {0}", o); } } /* Output: The value-type value = 456 The object-type value = 123 */ Over here it

boxing unboxing

非 Y 不嫁゛ 提交于 2019-12-02 09:42:36
I found the following code snippet while searching about boxing and unboxing in C#. class TestBoxing { static void Main() { int i = 123; // Boxing copies the value of i into object o. object o = i; // Change the value of i. i = 456; // The change in i does not effect the value stored in o. System.Console.WriteLine("The value-type value = {0}", i); System.Console.WriteLine("The object-type value = {0}", o); } } /* Output: The value-type value = 456 The object-type value = 123 */ Over here it says that even though he value of i changes the value of o remains the same.If so then o is referencing

How can I box the contents of an iterator of a type that implements a trait?

六月ゝ 毕业季﹏ 提交于 2019-12-02 02:10:53
I'm taking an iterator of some type that must implement the trait A , and trying to convert it into a Vec of Box es of that trait: trait A {} fn test2<'a, I>(iterator: I) -> Vec<Box<A + 'a>> where I: IntoIterator, I::Item: A + 'a, { iterator .into_iter() .map(|a| Box::new(a)) .collect::<Vec<Box<A + 'a>>>() } However, this fails to compile, saying: error[E0277]: the trait bound `std::vec::Vec<std::boxed::Box<A + 'a>>: std::iter::FromIterator<std::boxed::Box<<I as std::iter::IntoIterator>::Item>>` is not satisfied --> src/main.rs:11:10 | 11 | .collect::<Vec<Box<A + 'a>>>() | ^^^^^^^ a collection

Extending java Integer cache

瘦欲@ 提交于 2019-12-01 22:31:15
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? You can use the java.lang.Integer.IntegerCache.high property to increase the size of this cache. ex : java -Djava.lang.Integer.IntegerCache.high=4096 SomeClass.class My questions to you are: 1) Why is your code making

Comparing structs for equality without boxing

大城市里の小女人 提交于 2019-12-01 16:46:41
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. Assumptions: The first of the following methods is illegal since structs do not implicitly override the

Is casting to an interface a boxing conversion?

拥有回忆 提交于 2019-12-01 15:13:40
I have an interface IEntity public interface IEntity{ bool Validate(); } And I have a class Employee which implements this interface public class Employee : IEntity{ public bool Validate(){ return true; } } Now if I have the following code Employee emp1 = new Employee(); IEntity ent1 = (IEntity)emp1; // Is this a boxing conversion? If it is not a boxing conversion then how does the cast work? No, since Employee is a class, which is a reference type rather than a value type . From MSDN : Boxing is the process of converting a value type to the type object or to any interface type implemented by

Is casting to an interface a boxing conversion?

你说的曾经没有我的故事 提交于 2019-12-01 14:04:39
问题 I have an interface IEntity public interface IEntity{ bool Validate(); } And I have a class Employee which implements this interface public class Employee : IEntity{ public bool Validate(){ return true; } } Now if I have the following code Employee emp1 = new Employee(); IEntity ent1 = (IEntity)emp1; // Is this a boxing conversion? If it is not a boxing conversion then how does the cast work? 回答1: No, since Employee is a class, which is a reference type rather than a value type. From MSDN:

What is the use of into_boxed_slice() methods?

混江龙づ霸主 提交于 2019-12-01 05:57:35
问题 Looking at the methods available for Vec<T> I stumbled across into_boxed_slice(self) -> Box<[T]> String also has such a method ( into_boxed_str(self) ). The usefulness of having Deref for Vec<T> / String that allows them to be treated like a shared slice ( &[T] ) is obvious, but I don't see any use for an owned slice ( Box<[T]> ) except, perhaps, FFI. The Rust GitHub repo only uses into_boxed_slice() in a handful of cases. Since methods for creating boxed slices are available in std and this

Does boxing cause performance issues?

柔情痞子 提交于 2019-12-01 03:43:52
问题 I'm working on a project in which we are producing a language which compiles to java. The framework we are using (xtext) makes prolific use of boxing in its generated code. Specifically, if you have a statement like: int i = 1; int j = 2; int k = i + j; Then the compiled code looks like: IntegerExtensions.operator_plus(((Integer)i), ((Integer)j)) Now, in the project I'm working on, there are certain situations where particular basic binary operations are going to be extremely common

Details on what happens when a struct implements an interface

纵然是瞬间 提交于 2019-12-01 03:12:31
I recently came across this Stackoverflow question: When to use struct? In it, it had an answer that said something a bit profound: In addition, realize that when a struct implements an interface - as Enumerator does - and is cast to that implemented type, the struct becomes a reference type and is moved to the heap. Internal to the Dictionary class, Enumerator is still a value type. However, as soon as a method calls GetEnumerator(), a reference-type IEnumerator is returned. Exactly what does this mean? If I had something like struct Foo : IFoo { public int Foobar; } class Bar { public IFoo