public static void Test()
{ DateTime endDate = new DateTime(2011,1,1,23,59,59);
DateTime startDate= new DateTime(2011,1,1);
TimeSpan ts = endDate- startDate;
//TotalDays 是double 是會有小數的要小心
Console.WriteLine("TotalDays=" + ts.TotalDays);
//Days 是int ,如果只是單純計算天數差別,用Days是正解
Console.WriteLine("Days=" + ts.Days);}
Result:
==================================================
TotalDays=0.999988425925926
Days=0
==================================================
參考: http://msdn.microsoft.com/zh-tw/library/system.timespan.days(v=vs.95).aspx
丫烈客 發表在 痞客邦 留言(0) 人氣(1,235)
在config檔裡加上runtime 的區塊,如下<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="bin"/>
</assemblyBinding>
</runtime>
</configuration>privatePath屬性,可以指定多個資料夾,用分號區隔開,但資料夾位置只能在應用程式的子路徑下。
例:<probing privatePath="bin;bin2\subbin;bin3"/>
也可以利用程式的方式加入
AppDomain.CurrentDomain.AppendPrivatePath("bin");
AppDomain.CurrentDomain.AppendPrivatePath("bin2");
MSDN 參考資料
http://msdn.microsoft.com/en-us/library/system.appdomain.relativesearchpath.aspx 丫烈客 發表在 痞客邦 留言(0) 人氣(1,040)
Stopwatch 是 .NET 2.0中提供的新類別 可以用來測量時間. MSDN提供的範例,如下: using System;
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main(string[] args)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000);
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed; // Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine(elapsedTime, "RunTime");
}
}
丫烈客 發表在 痞客邦 留言(0) 人氣(200)
.NET Framework 2.0 新增的重要的功能 "泛型"
而List<T> 是ArrayList 的泛型類別.
ArrayList 是一種非常好用的集合,可以放入任何類型的資料,又不用跟Array一樣要事先宣告長度. 但是這樣太過於開放有時候會造成一些困擾.例如你需要讓集合中的資料一定要某些型別,但是又不知道集合的長度.這時候就可以使用ArrayList的泛型類別 List<T>.
List<T> = 必須指定型別的Array List
<T> 表示型別的意思
例如:
List<string> list =new List<string>();
list.Add("1");
string str= list[0] ;
用List<string> 的好處是
1.放入資料時,可以限定型別,這樣一來如果放錯型別在編譯階段就會被發現.
2.取出時不用再轉型,可以增加程式的效能.
當然 SortedList 也有對應的泛型類別 SortedList<TKey,TValue>
丫烈客 發表在 痞客邦 留言(0) 人氣(567)
記得在某次教育訓練時,講師有提到
SQL connection 的處理 一般人都不會注意 connection 會因為程式錯誤而造成沒有close 的問題
當時建議
用 try finally 來解決.
try{
connection.open();
}
finally
{
connection.close();
}
現在發現更簡單的語法, 用 using 也可以達成同樣的效果
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.open();
}
底下說明來自MSDN
C# 程式語言的 using 陳述式會藉由簡化您必須為建立和清除物件而撰寫的程式碼,以較為自動的方式呼叫 Dispose 方法。Using 陳述式會取得一項或多項資源、執行您指定的陳述式,然後處置該物件。請注意,using 陳述式對於物件之存留期不超過建構該物件之方法的物件才有用。下列程式碼範例會建立並清除 ResourceWrapper 類別的執行個體,一如之前在 C# 實作 Dispose 方法範例中所提供的說明。
class myApp
{
public static void Main()
{
using (ResourceWrapper r1 = new ResourceWrapper())
{
// Do something with the object.
r1.DoSomething();
}
}
}
前面含有 using 陳述式的程式碼,就相當於下列情形。
class myApp
{
public static void Main()
{
ResourceWrapper r1 = new ResourceWrapper();
try
{
// Do something with the object.
r1.DoSomething();
}
finally
{
// Check for a null resource.
if (r1 != null)
// Call the object's Dispose method.
r1.Dispose();
}
}
}
丫烈客 發表在 痞客邦 留言(0) 人氣(6,546)