boxing

Improve efficiency of database row reading without boxing

心不动则不痛 提交于 2019-12-25 09:07:46
问题 I use a boxing approach to read data from database via a type-switch helper class described here. The boxing approach is mainly used for custom types that I derive from/to default types (e.g. I have a DB_Image element stored as an int32 value, which correspongs to the index in a image list). I discovered how bad this approach was in terms of performance. The question is, can I do better? Examples welcome. 回答1: Ok, so I just went to write a little test program about different approaches to

No Boxing Conversion for two type parameters shared Parent and Child

两盒软妹~` 提交于 2019-12-24 17:00:39
问题 I get the following error (VSC#2010 Express) on the declaration of GetChild method... Error 1 The type 'T' cannot be used as type parameter 'T' in the generic type or method '...Child'. There is no boxing conversion or type parameter conversion from 'T' to '...IParent'. public interface IParent<T, Id> { Child<T, Id> GetChild(); } public class Child<T, Id> where T : IParent<T, Id> { public T Parent; } I want any class to inherit IParent, and for each such class to construct a member instance

Questions about boxing

梦想与她 提交于 2019-12-24 05:48:10
问题 I know that boxing is a popular concept with plenty of information available on it, but I have a few questions which I can't really find answers to: 1) If boxing leads to a value type (struct) being converted to an object (Reference type), or reference type, then why use a value type which will be boxed and incur a performance penalty? I am aware of the benefits and suitability in certain cases of either a struct or class. It is said (1) that values (value types) tend to live on the stack in

Inconsistent null equality check scala 2.11.7

旧时模样 提交于 2019-12-23 16:28:00
问题 Edit: This issue no longer exists in Scala 2.12.6 Original question (for Scala 2.11.7): Why so strange warning? scala> null.asInstanceOf[Double] res0: Double = 0.0 scala> null.asInstanceOf[Double] == null <console>:11: warning: comparing values of types Double and Null using `==' will always yield !!!!false!!!! null.asInstanceOf[Double] == null ^ res1: Boolean = true //!!!! scala> 0.0 == null <console>:11: warning: comparing values of types Double and Null using `==' will always yield false 0

Why does scalac need to box an `Int` in a method expecting an `Any`

让人想犯罪 __ 提交于 2019-12-23 12:44:01
问题 Consider the following class: package test class Test { def main(args: Array[String]): Unit = { val i: Int = 0 println(i) } } The bytecode of main is: public main([Ljava/lang/String;)V // parameter final args L0 LINENUMBER 4 L0 ICONST_0 L1 ISTORE 2 L2 LINENUMBER 5 L2 GETSTATIC scala/Predef$.MODULE$ : Lscala/Predef$; ILOAD 2 INVOKESTATIC scala/runtime/BoxesRunTime.boxToInteger (I)Ljava/lang/Integer; INVOKEVIRTUAL scala/Predef$.println (Ljava/lang/Object;)V L3 RETURN L4 LOCALVARIABLE i I L1 L3

How can I define a function with a parameter that can be multiple kinds of trait objects?

坚强是说给别人听的谎言 提交于 2019-12-23 09:16:18
问题 I'm trying to define a function that will take a reference as a parameter, and call a generic method on the referenced object, passing in a concrete value. I need a way of requiring that the generic type of the parameter passed to my function is a trait of the concrete type that the function will use it with. I can't seem to work out how to do this. A minimal example of the sort of thing I'm trying to achieve: trait Vehicle {} trait Floating {} struct Boat; impl Vehicle for Boat {} impl

Value types inferred as object at runtime when using dynamic

不羁岁月 提交于 2019-12-22 11:37:20
问题 I almost understand why this particular problem arises (though I more than welcome a layman's explanation if you can find the time!), it I'm sure involves boxing/unboxing which I won't attempt to incorrectly explain.. With my current knowledge (or lack thereof), of the situation, I'm not sure how best to proceed to resolve it. Here is a fairly simplified console app showing my issue: static void Main(string[] args) { try { // succeeds IEnumerable<Expression<Func<TestCase1Impl, dynamic>>>

Casting Exception when trying to get value from ExecuteScalar()

ⅰ亾dé卋堺 提交于 2019-12-22 04:46:17
问题 In the below code, the statement 1 throws casting exception. I am wondering why isnt it unboxing? The statement 2 works fine but I want to know why the first one is wrong? using (IDbCommand command = connection.CreateCommand()) { command.CommandText = string.Format("SELECT COUNT(1) FROM {0}", tableName); int count = (int)command.ExecuteScalar(); //statement 1 } //int count = Convert.ToInt32(command.ExecuteScalar()); //statement 2 回答1: Sigh, execute scalar returns a long/int64, seeing as you

How is Nullable<T> different from a similar custom C# struct?

倖福魔咒の 提交于 2019-12-21 18:13:15
问题 In Nullable micro-optimizations, part one, Eric mentions that Nullable<T> has a strange boxing behaviour that could not be achieved by a similar user-defined type. What are the special features that the C# language grants to the predefined Nullable<T> type? Especially the ones that could not be made to work on a MyNullable type. Of course, Nullable<T> has special syntactic sugar T? , but my question is more about semantics. 回答1: What I was getting at is: there is no such thing as a boxed

How is Nullable<T> different from a similar custom C# struct?

你。 提交于 2019-12-21 18:13:06
问题 In Nullable micro-optimizations, part one, Eric mentions that Nullable<T> has a strange boxing behaviour that could not be achieved by a similar user-defined type. What are the special features that the C# language grants to the predefined Nullable<T> type? Especially the ones that could not be made to work on a MyNullable type. Of course, Nullable<T> has special syntactic sugar T? , but my question is more about semantics. 回答1: What I was getting at is: there is no such thing as a boxed