今天考試又被問到對Unity從來沒有查明的問題,就是Monobehaviour裡面原本被已經決定好的function的執行順序,像是Awake或者Start之類的,原本以為只有一些簡單的Awake、Start、OnEnable、Update、OnDisable、OnDestroy這幾個,查一下Unity官網,想不到還真多。
唯一讓我覺得奇怪的是Start竟然比OnEnable晚,真是奇怪.....
2015年3月24日 星期二
2015年3月9日 星期一
C# value type or reference type
在C# type內有些data type常常搞錯,不知道是value type或者reference type,因為考試又被電一次,在這備註一下。
主要是reference type才有可能為null。
主要是reference type才有可能為null。
string testValue;
if(testVlaue != null)
{
//是否會進來
}
在這邊因為string是reference type,只是他的operator"="被改寫成是用copy value的方式,看起來很像value type。所以這邊的if不會進來。decimal testValue;
if(testVlaue != null)
{
//是否會進來
}
decimal是value type所以這個if永遠都會進來。DateTime testValue;
if(testVlaue != null)
{
//是否會進來
}
DateTime這個data type也是令人意外的type,他是value type,所以這個if永遠都會進來。int? testValue;
if(testVlaue != null)
{
//是否會進來
}
這個int?就是reference type,所以這個if不會進來。
Tread start problem
今天考試被電了一回,題目是
public class A
{
public int count = 0;
public void AddCount(int value)
{
count += value;
}
}
public void test()
{
A recordA = new A();
Threand thread = new Thread(delegate()
{
recordA.AddCount(1);
}
thread.Start();
recordA.AddCount(1);
}
這邊是因為thread.Start()時,就有可能和下一行recordA.AddCount(1);同時執行,並且在拿count這個變數的值時,有可能都拿到相同的值。所以這邊應該在AddCount function內加lock。
訂閱:
意見 (Atom)