Dictionary<int, ArrayList> Deduction_Employees =
new Dictionary<int, ArrayList>();
Run Code Online (Sandbox Code Playgroud)
后来我在这个数组列表中添加了这样的匿名类型
var day_and_type = new {
TheDay = myDay,
EntranceOrExit = isEntranceDelay
};
Deduction_Employees[Employee_ID].Add(day_and_type);
Run Code Online (Sandbox Code Playgroud)
现在我如何取消打开var并访问这些属性?
我试图弄清楚当类型本身未知时支持将整数类型(short/int/long)拆箱到其固有类型的语法.
这是一个完全人为的例子,它展示了这个概念:
// Just a simple container that returns values as objects
struct DataStruct
{
public short ShortVale;
public int IntValue;
public long LongValue;
public object GetBoxedShortValue() { return ShortVale; }
public object GetBoxedIntValue() { return IntValue; }
public object GetBoxedLongValue() { return LongValue; }
}
static void Main( string[] args )
{
DataStruct data;
// Initialize data - any value will do
data.LongValue = data.IntValue = data.ShortVale = 42;
DataStruct newData;
// This works if you know the type …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我收到一个编译错误,指出我的'x'类型不匹配:
val someRef: java.lang.Long = 42L
someRef match {
case x: Long => println("The answer: " + x)
case _ => println("Unknown")
}
Run Code Online (Sandbox Code Playgroud)
如何让Scala在match语句中自动取消装箱someRef?
我读了什么是拳击和拆箱,有什么权衡?但无法理解一件事.假设我有一个班级:
class MyClass
{
public int Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想在我的方法中获得价值:
void MyFunc(MyClass cls)
{
int i = cls.Value;
}
Run Code Online (Sandbox Code Playgroud)
作为一个放在堆中的类,我猜那个值也放在堆中了吗?因此操作
int i = cls.Value;
Run Code Online (Sandbox Code Playgroud)
拆箱?或者它不是拆箱?
我一直试图理解这一段,但不知怎的,我无法在脑海中虚拟化,有些人请详细说明一下:
拆箱与拳击并不完全相反.拆箱操作比拳击便宜得多.拆箱实际上只是获取指向对象中包含的原始值类型(数据字段)的指针的操作.实际上,指针指的是盒装实例中的未装箱部分.因此,与装箱不同,拆箱不涉及复制内存中的任何字节.在做出这个重要的澄清之后,重要的是要注意通常在取消装箱操作之后复制字段.
里希特,杰弗里(2010-02-05).CLR通过C#(Kindle Locations 4167-4171).OReilly Media - A. Kindle版.
下面是一些快速代码来说明我的问题.有什么方法可以避免这种明显不必要的拳击/拆箱?
public class TestClass<T>
{
public T TestMethod()
{
if (typeof(T) == typeof(bool))
{
return true; // doesn't work
return (T)(object)true; // works, but any way to avoid this?
}
return default(T);
}
}
Run Code Online (Sandbox Code Playgroud) 我无法找到适合如何使我的数据类型取消装箱的良好资源,以便在未装箱的矢量中使用.我如何制作数据类型
data Color = Yellow | Red | Green | Blue | Empty deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)
是Unbox的一个实例?
编辑:经过更多的探讨后,似乎通过强制某些函数中的参数是严格的,我可以说服GHC自动取消装箱.如果这适用于我的情况?我怎么知道哪些参数要严格?
例如,看看这段代码:
Integer myInt = new Integer(5);
int i1 = myInt.intValue();
int i2 = myInt;
System.out.println(i1);
System.out.println(i2);
Run Code Online (Sandbox Code Playgroud)
如您所见,我有两种方法可以将包装器中的整数值复制到原始值:
我可以使用拆箱
要么
我可以使用方法intValue()
那么......当已经取消装箱时,有什么方法需要一个方法?
假设我有一个简单的数据类型,如:
data Cell = Open | Blocked
Run Code Online (Sandbox Code Playgroud)
我想用一个UArray Int Cell.是否有捷径可寻?我可以以某种方式重用该定义UArray Int Bool吗?
我正在关注Android图书示例:
//Get the drink from the intent
int drinkIdd = (Integer)getIntent().getExtras().get(EXTRA_DRINKID);
Drink drink = Drink.drinks[drinkIdd];
Run Code Online (Sandbox Code Playgroud)
该项目可以在Android Studio中运行,但在线上带有黄色警告:
int drinkIdd = (Integer)getIntent().getExtras().get(EXTRA_DRINKID);
Run Code Online (Sandbox Code Playgroud)
与:
info: Unboxing of '(Integer)getIntent().getExtras().get(EXTRA_DRINKID)' may produce 'java.lang.NullPointerException'
Run Code Online (Sandbox Code Playgroud)
根据我的理解,get(EXTRA_DRINKID)返回一个Object,然后(Integer)将其转换为int类型,以与满足int drinkIdd。
我可以这样写上限吗?使用()将getIntent()。getExtras()。get()打包为一个整体,因为它最终返回了一个对象,然后将其转换为int。
int drinkIdd = (Integer)(getIntent().getExtras().get(EXTRA_DRINKID));
Run Code Online (Sandbox Code Playgroud)