boxing

Variable number of arguments without boxing the value-types?

随声附和 提交于 2019-12-06 19:50:29
问题 public void DoSomething(params object[] args) { // ... } The problem with the above signature is that every value-type that will be passed to that method will be boxed implicitly, and this is serious performance issue for me. Is there a way to declear a method that accepts variable number of arguments without boxing the value-types? Thanks. 回答1: You can use generics: public void DoSomething<T>(params T[] args) { } However, this will only allow a single type of ValueType to be specified. If

Value types inferred as object at runtime when using dynamic

戏子无情 提交于 2019-12-06 10:31:42
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>>> results1 = typeof(ITestCase1).GetMethods().Select(m => buildDynamicExpression(new TestCase1Impl(), m));

Checking Null Wrappers against primitive values

女生的网名这么多〃 提交于 2019-12-06 07:58:48
Integer i = null; if (i == 3) Why the second line above throws a NullPointerException , IMHO, this has only one meaning which is Wrapper Object i is to be unboxed which yields the Exception such as: ArrayList<Integer> list = new ArrayList<Integer>(); list.add(null); int x = list.get(0); EDIT: Can you supply me with some format doc? It throws NPE because compiler does the following "magic" for you: Integer i = null; if (i.intValue() == 3) Obviously i.intValue() throws NPE when i is null . Think of the wrapper class to be a holder object. Something like: public class Integer { private int

Why auto-boxing marked as a warning?

别来无恙 提交于 2019-12-05 14:20:00
问题 I understand that auto un-boxing should be done with care because the reference that is being un-boxed can be null. Why is auto-boxing marked as warning as well? Are there some pitfalls I am missing here? 回答1: If you don't expect performance issues (in terms of micro-optimization) you can safely disable this warning. It is just an indication in case you're not aware that auto-boxing happends here. In business-logic code where you have I/O overhead (due to DB transactions or disc access) auto

Boxing and unboxing when using out and ref parameters

风格不统一 提交于 2019-12-05 10:33:10
问题 Does boxing/unboxing occur when a method accepts an out/ref parameter of a ValueType? 回答1: 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

Is converting this ArrayList to a Generic List efficient?

流过昼夜 提交于 2019-12-05 10:18:20
The code I'm writing receives an ArrayList from unmanaged code, and this ArrayList will always contain one or more objects of type Grid_Heading_Blk. I've considered changing this ArrayList to a generic List, but I'm unsure if the conversion operation will be so expensive as to nullify the benefits of working with the generic list. Currently, I'm just running a foreach (Grid_Heading_Blk in myArrayList) operation to work with the ArrayList contents after passing the ArrayList to the class that will use it. Should I convert the ArrayList to a generic typed list? And if so, what is the most

Casting Exception when trying to get value from ExecuteScalar()

旧巷老猫 提交于 2019-12-05 05:36:47
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 Sigh, execute scalar returns a long/int64, seeing as you wrote the SQL command you know the return vale is going to be a computed whole number (SELECT COUNT(..., also

How to create a vector of boxed closures in Rust?

瘦欲@ 提交于 2019-12-05 05:26:17
Previously a question was asked about creating an array of functions where the functions returned integers from a range. The final solution was to do a map/collect into a Vec<_> . I have a similar yet different situation where I have closures with the same signature but different implementations. I tried this: let xs: Vec<_> = vec![ move |(x, y)| (y, x), move |(x, y)| (1 - y, 1 - x), ]; The error I get back: error[E0308]: mismatched types --> src/main.rs:4:9 | 4 | move |(x, y)| (1 - y, 1 - x), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected closure, found a different closure | = note: expected type `

Variable number of arguments without boxing the value-types?

偶尔善良 提交于 2019-12-05 02:48:49
public void DoSomething(params object[] args) { // ... } The problem with the above signature is that every value-type that will be passed to that method will be boxed implicitly, and this is serious performance issue for me. Is there a way to declear a method that accepts variable number of arguments without boxing the value-types? Thanks. You can use generics: public void DoSomething<T>(params T[] args) { } However, this will only allow a single type of ValueType to be specified. If you need to mix or match value types, you'll have to allow boxing to occur, as you're doing now, or provide

How to store structs of different types without boxing

筅森魡賤 提交于 2019-12-05 02:26:56
I'm creating a messaging system for use in an XNA game. My Message types are structs because I want them to behave in a Value Type way. struct MyMessageType1 : IMessage {} struct MyMessageType2 : IMessage {} List<IMessage> messageQueue = new List<IMessage>(); I want to be able to store Messages of different types in my message queue, but I want to do so without any of them being boxed. If I have the structs implement an interface such as IMessage and I try to store them in a List, they get boxed. I don't know all the possible message types ahead of time, so I can't just hard code one List for