boxing

Boxing error with generic function

一世执手 提交于 2020-01-07 03:40:23
问题 If I comment out my static class this compiles fine. However i'd like to have the static class working so I can use the first commented out line in main. My error is error CS0314: The type 'T' cannot be used as type parameter 'T' in the generic type or method 'DateTest.Range'. There is no boxing conversion or type parameter conversion from 'T' to 'System.IComparable'. My source is using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading

关于HashMap的一个有趣的小程序

纵然是瞬间 提交于 2020-01-07 02:54:02
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> import java.util.*; public class TestArgsWords { private static final Integer ONE = new Integer(1); public static void main(String[] args) { Map<String, Integer> m = new HashMap<>(); for (int i = 0; i < args.length; i++) { Integer freq = m.get(args[i]); m.put(args[i], freq == null ? ONE : new Integer(freq.intValue() + 1)); } System.out.println( m.size() + " distinct words detected:"); System.out.println(m); } } 或者利用 auto_boxing / auto_unboxing 机制写成: import java.util.*; public class TestArgsWords { private static final int ONE = 1; public static void main

IronPython: How to call a function that expects an array of value-types?

点点圈 提交于 2020-01-05 02:13:04
问题 I have come across a problem with IronPython that I can't solve. I need to call a function that takes a parameter of type array to value-type. The function-signature (in C++/CLI notation) is this: static int PLGServiceInterface::PLGService::MapLowSpeedRealtimeNames(cli::array<System::String ^> ^ SignalNames, int timeout_ms, cli::array<PLGServiceInterface::LVCluster_1> ^% Channels, System::String ^% ReportText) When I call the function from IronPython import clr clr.AddReferenceToFileAndPath(

Scala 2.10, Double.isNaN, and boxing

邮差的信 提交于 2020-01-03 15:22:07
问题 In Scala 2.10, is someDouble.isNaN expected to box? Running my code calling .isNaN through a decompiler, I still see telltale calls to double2Double in my code. Given the new AnyVal work in 2.10, I'd expect it to be no worse than java.lang.Double.isNaN(someDouble) at runtime with no spurious allocations. Am I missing something? 回答1: Unfortunately, isNaN is a method on java.lang.Double , and it is essential to have an implicit conversion to java.lang.Double , so the Scala RichDouble value

Type Casting an Object using a “Type” Object in C#

只谈情不闲聊 提交于 2020-01-03 07:26:09
问题 This one has proven to be a little tricky for me so far. I am wondering if it is possible to type cast an object using a System.Type object. I have illustrated below what I mean: public interface IDataAdapter { object Transform(object input); Type GetOutputType(); } public class SomeRandomAdapter : IDataAdapter { public object Transform(object input) { string output; // Do some stuff to transform input to output... return output; } public Type GetOutputType() { return typeof(string); } } //

How to store structs of different types without boxing

▼魔方 西西 提交于 2020-01-02 01:41:11
问题 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

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

ぃ、小莉子 提交于 2019-12-31 03:00:15
问题 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

Integer auto-unboxing and auto-boxing gives performance issues?

て烟熏妆下的殇ゞ 提交于 2019-12-30 17:23:30
问题 We are currently doing some iterations and other operations using x++; where x is an Integer and not an int . Operations may be repeated throughout some user operations on our system but nothing too complex or numerous like a Mathematical application, maximum up to 10000 times per user transaction.. Will this unboxing and later boxing affect our performance by some noticeable milliseconds ? 回答1: http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html "The performance of

Integer auto-unboxing and auto-boxing gives performance issues?

拈花ヽ惹草 提交于 2019-12-30 17:22:52
问题 We are currently doing some iterations and other operations using x++; where x is an Integer and not an int . Operations may be repeated throughout some user operations on our system but nothing too complex or numerous like a Mathematical application, maximum up to 10000 times per user transaction.. Will this unboxing and later boxing affect our performance by some noticeable milliseconds ? 回答1: http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html "The performance of

Why Enum's HasFlag method need boxing?

烂漫一生 提交于 2019-12-28 14:41:29
问题 I am reading "C# via CLR" and on page 380, there's a note saying the following: Note The Enum class defines a HasFlag method defined as follows public Boolean HasFlag(Enum flag); Using this method, you could rewrite the call to Console.WriteLine like this: Console.WriteLine("Is {0} hidden? {1}", file, attributes.HasFlag(FileAttributes.Hidden)); However, I recommend that you avoid the HasFlag method for this reason: Since it takes a parameter of type Enum, any value you pass to it must be