- Console.WriteLine(DateTime.Now.ToUniversalTime().AddHours(8).ToShortTimeString());
- //TimeSpan
- DateTime t1 = new DateTime(1980,1,1,0,0,0);
- DateTime t2 = DateTime.Now;
- //int spent = new TimeSpan(t2.Ticks - t1.Ticks).Hours;
- TimeSpan ts = t2 - t1;
- Console.WriteLine(ts.Days);
2014年1月31日 星期五
[C#] TimeSpan 練習
[C#] string 多行的時候 用@
來源 : Multi-Line Strings in C#
怕忘記提醒一下自己~
燈燈燈 ~
怕忘記提醒一下自己~
string sql = "SELECT foo " +
"FROM bar " + "WHERE baz=42";燈燈燈 ~
string sql = @"SELECT foo
FROM bar WHERE baz=42";
[C#] out
MSDN 範例
當您想讓一個方法傳回多個值時,宣告 out 方法十分有用。使用 out 參數的方法依然可以將變數當做傳回型別進行存取 (請參閱 return),但它也可將一個或多個物件當做 out 參數傳回至呼叫方法。此範例使用 out 單一呼叫方法以傳回三個變數。請注意,第三個引數是指派為 null。這樣可讓方法能夠選擇性地傳回值。
**out適合用在需要retrun多個回傳值的地方,而ref則用在修改呼叫者丟進的參數時。
**out 的值,一開始可以不用設定值
**out的值進到函數就必須設定值,不然會出錯
當您想讓一個方法傳回多個值時,宣告 out 方法十分有用。使用 out 參數的方法依然可以將變數當做傳回型別進行存取 (請參閱 return),但它也可將一個或多個物件當做 out 參數傳回至呼叫方法。此範例使用 out 單一呼叫方法以傳回三個變數。請注意,第三個引數是指派為 null。這樣可讓方法能夠選擇性地傳回值。
**out適合用在需要retrun多個回傳值的地方,而ref則用在修改呼叫者丟進的參數時。
**out 的值,一開始可以不用設定值
**out的值進到函數就必須設定值,不然會出錯
- class OutReturnExample
- {
- static void Method(out int i, out string s1, out string s2)
- {
- i = 44;
- s1 = "I've been returned";
- s2 = null;
- }
- static void Main()
- {
- int value;
- string str1, str2;
- Method(out value, out str1, out str2);
- // value is now 44
- // str1 is now "I've been returned"
- // str2 is (still) null;
- }
- }
[C#] Ditionary
MSDN 範例
補充 : 初始化值
new Ditionary<TKey,TValue>()
{
{"xxx","xxx"},{"yyy","yyy"},
{"zzz","zzz"},{"aaa","aaa"}
}
補充 : 初始化值
new Ditionary<TKey,TValue>()
{
{"xxx","xxx"},{"yyy","yyy"},
{"zzz","zzz"},{"aaa","aaa"}
}
- using System;
- using System.Collections.Generic;
- public class Example
- {
- public static void Main()
- {
- // Create a new dictionary of strings, with string keys.
- //
- Dictionary<string, string> openWith =
- new Dictionary<string, string>();
- // Add some elements to the dictionary. There are no
- // duplicate keys, but some of the values are duplicates.
- openWith.Add("txt", "notepad.exe");
- openWith.Add("bmp", "paint.exe");
- openWith.Add("dib", "paint.exe");
- openWith.Add("rtf", "wordpad.exe");
- // The Add method throws an exception if the new key is
- // already in the dictionary.
- try
- {
- openWith.Add("txt", "winword.exe");
- }
- catch (ArgumentException)
- {
- Console.WriteLine("An element with Key = \"txt\" already exists.");
- }
- // The Item property is another name for the indexer, so you
- // can omit its name when accessing elements.
- Console.WriteLine("For key = \"rtf\", value = {0}.",
- openWith["rtf"]);
- // The indexer can be used to change the value associated
- // with a key.
- openWith["rtf"] = "winword.exe";
- Console.WriteLine("For key = \"rtf\", value = {0}.",
- openWith["rtf"]);
- // If a key does not exist, setting the indexer for that key
- // adds a new key/value pair.
- openWith["doc"] = "winword.exe";
- // The indexer throws an exception if the requested key is
- // not in the dictionary.
- try
- {
- Console.WriteLine("For key = \"tif\", value = {0}.",
- openWith["tif"]);
- }
- catch (KeyNotFoundException)
- {
- Console.WriteLine("Key = \"tif\" is not found.");
- }
- // When a program often has to try keys that turn out not to
- // be in the dictionary, TryGetValue can be a more efficient
- // way to retrieve values.
- string value = "";
- if (openWith.TryGetValue("tif", out value))
- {
- Console.WriteLine("For key = \"tif\", value = {0}.", value);
- }
- else
- {
- Console.WriteLine("Key = \"tif\" is not found.");
- }
- // ContainsKey can be used to test keys before inserting
- // them.
- if (!openWith.ContainsKey("ht"))
- {
- openWith.Add("ht", "hypertrm.exe");
- Console.WriteLine("Value added for key = \"ht\": {0}",
- openWith["ht"]);
- }
- // When you use foreach to enumerate dictionary elements,
- // the elements are retrieved as KeyValuePair objects.
- Console.WriteLine();
- foreach( KeyValuePair<string, string> kvp in openWith )
- {
- Console.WriteLine("Key = {0}, Value = {1}",
- kvp.Key, kvp.Value);
- }
- // To get the values alone, use the Values property.
- Dictionary<string, string>.ValueCollection valueColl =
- openWith.Values;
- // The elements of the ValueCollection are strongly typed
- // with the type that was specified for dictionary values.
- Console.WriteLine();
- foreach( string s in valueColl )
- {
- Console.WriteLine("Value = {0}", s);
- }
- // To get the keys alone, use the Keys property.
- Dictionary<string, string>.KeyCollection keyColl =
- openWith.Keys;
- // The elements of the KeyCollection are strongly typed
- // with the type that was specified for dictionary keys.
- Console.WriteLine();
- foreach( string s in keyColl )
- {
- Console.WriteLine("Key = {0}", s);
- }
- // Use the Remove method to remove a key/value pair.
- Console.WriteLine("\nRemove(\"doc\")");
- openWith.Remove("doc");
- if (!openWith.ContainsKey("doc"))
- {
- Console.WriteLine("Key \"doc\" is not found.");
- }
- }
- }
- /* This code example produces the following output:
- An element with Key = "txt" already exists.
- For key = "rtf", value = wordpad.exe.
- For key = "rtf", value = winword.exe.
- Key = "tif" is not found.
- Key = "tif" is not found.
- Value added for key = "ht": hypertrm.exe
- Key = txt, Value = notepad.exe
- Key = bmp, Value = paint.exe
- Key = dib, Value = paint.exe
- Key = rtf, Value = winword.exe
- Key = doc, Value = winword.exe
- Key = ht, Value = hypertrm.exe
- Value = notepad.exe
- Value = paint.exe
- Value = paint.exe
- Value = winword.exe
- Value = winword.exe
- Value = hypertrm.exe
- Key = txt
- Key = bmp
- Key = dib
- Key = rtf
- Key = doc
- Key = ht
- Remove("doc")
- Key "doc" is not found.
- */
訂閱:
文章 (Atom)