2014年1月31日 星期五

[C#] 題目練習Q&A part 2.

7.編譯頁面上所有TextBox控制項並給它賦值為ABC

  1.  foreach (TextBox txt in Form.Controls.OfType<TextBox>())
  2.             {
  3.                 txt.Text = "ABC";
  4.             }


8.用.net做B/S結構的系統,您是用幾層結構來開發,每一層之間的關系以及為什么要這樣分層?
答︰一般為3層
數據存取層,業務層,呈現層。
數據訪問層對數據庫進行增刪查改。
業務層一般分為二層,業務層實作與呈現層的溝通,業務規則層實現用戶密碼的安全等。
呈現層與用戶互動,如用戶添加表單。

好處是易於維護、調整,具有可擴展性。

9.此範例中 , NEW B 會出現甚麼結果??
  1. public class A
  2.     {
  3.         public A()
  4.         {
  5.             PrintFields();
  6.         }
  7.         public virtual void PrintFields()
  8.         {
  9.             HttpContext.Current.Response.Write("???");
  10.         }
  11.     }
  12.     public class B : A
  13.     {
  14.         int x = 1;
  15.         int y;
  16.         public B()
  17.         {
  18.             y = -1;
  19.         }
  20.         public override void PrintFields()
  21.         {
  22.             HttpContext.Current.Response.Write(string.Format("x={0},y={1}", x, y));
  23.         }
  24.     }

答 : x=1,y=0
B在建構時會先呼叫基礎類別的建構函示。

10.什么是受管製的代碼?
答︰unsafe︰非托管代碼。不經過CLR運行。

CLR: common language runtime

11. string a =null  與 string b = "" 差別
a 是指向NULL,並沒有產生實體heap記憶體。
b是指向一個heap記憶體,而內容是一個空字串。

[C#] TimeSpan 練習


  1. Console.WriteLine(DateTime.Now.ToUniversalTime().AddHours(8).ToShortTimeString());

  2.             //TimeSpan
  3.             DateTime t1 = new DateTime(1980,1,1,0,0,0);
  4.             DateTime t2 = DateTime.Now;
  5.             //int spent = new TimeSpan(t2.Ticks - t1.Ticks).Hours;
  6.             TimeSpan ts = t2 - t1;
  7.             Console.WriteLine(ts.Days);

[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的值進到函數就必須設定值,不然會出錯

  1. class OutReturnExample
  2. {
  3.     static void Method(out int i, out string s1, out string s2)
  4.     {
  5.         i = 44;
  6.         s1 = "I've been returned";
  7.         s2 = null;
  8.     }
  9.     static void Main()
  10.     {
  11.         int value;
  12.         string str1, str2;
  13.         Method(out valueout str1, out str2);
  14.         // value is now 44
  15.         // str1 is now "I've been returned"
  16.         // str2 is (still) null;
  17.     }
  18. }

[C#] Ditionary

MSDN 範例

補充 : 初始化值

new Ditionary<TKey,TValue>()
{
 {"xxx","xxx"},{"yyy","yyy"},
{"zzz","zzz"},{"aaa","aaa"}
}



  1. using System;
  2. using System.Collections.Generic;
  3. public class Example
  4. {
  5.     public static void Main()
  6.     {
  7.         // Create a new dictionary of strings, with string keys.
  8.         //
  9.         Dictionary<stringstring> openWith =
  10.             new Dictionary<stringstring>();
  11.         // Add some elements to the dictionary. There are no
  12.         // duplicate keys, but some of the values are duplicates.
  13.         openWith.Add("txt""notepad.exe");
  14.         openWith.Add("bmp""paint.exe");
  15.         openWith.Add("dib""paint.exe");
  16.         openWith.Add("rtf""wordpad.exe");
  17.         // The Add method throws an exception if the new key is
  18.         // already in the dictionary.
  19.         try
  20.         {
  21.             openWith.Add("txt""winword.exe");
  22.         }
  23.         catch (ArgumentException)
  24.         {
  25.             Console.WriteLine("An element with Key = \"txt\" already exists.");
  26.         }
  27.         // The Item property is another name for the indexer, so you
  28.         // can omit its name when accessing elements.
  29.         Console.WriteLine("For key = \"rtf\", value = {0}.",
  30.             openWith["rtf"]);
  31.         // The indexer can be used to change the value associated
  32.         // with a key.
  33.         openWith["rtf"] = "winword.exe";
  34.         Console.WriteLine("For key = \"rtf\", value = {0}.",
  35.             openWith["rtf"]);
  36.         // If a key does not exist, setting the indexer for that key
  37.         // adds a new key/value pair.
  38.         openWith["doc"] = "winword.exe";
  39.         // The indexer throws an exception if the requested key is
  40.         // not in the dictionary.
  41.         try
  42.         {
  43.             Console.WriteLine("For key = \"tif\", value = {0}.",
  44.                 openWith["tif"]);
  45.         }
  46.         catch (KeyNotFoundException)
  47.         {
  48.             Console.WriteLine("Key = \"tif\" is not found.");
  49.         }
  50.         // When a program often has to try keys that turn out not to
  51.         // be in the dictionary, TryGetValue can be a more efficient
  52.         // way to retrieve values.
  53.         string value = "";
  54.         if (openWith.TryGetValue("tif"out value))
  55.         {
  56.             Console.WriteLine("For key = \"tif\", value = {0}."value);
  57.         }
  58.         else
  59.         {
  60.             Console.WriteLine("Key = \"tif\" is not found.");
  61.         }
  62.         // ContainsKey can be used to test keys before inserting
  63.         // them.
  64.         if (!openWith.ContainsKey("ht"))
  65.         {
  66.             openWith.Add("ht""hypertrm.exe");
  67.             Console.WriteLine("Value added for key = \"ht\": {0}",
  68.                 openWith["ht"]);
  69.         }
  70.         // When you use foreach to enumerate dictionary elements,
  71.         // the elements are retrieved as KeyValuePair objects.
  72.         Console.WriteLine();
  73.         foreach( KeyValuePair<stringstring> kvp in openWith )
  74.         {
  75.             Console.WriteLine("Key = {0}, Value = {1}",
  76.                 kvp.Key, kvp.Value);
  77.         }
  78.         // To get the values alone, use the Values property.
  79.         Dictionary<stringstring>.ValueCollection valueColl =
  80.             openWith.Values;
  81.         // The elements of the ValueCollection are strongly typed
  82.         // with the type that was specified for dictionary values.
  83.         Console.WriteLine();
  84.         foreach( string s in valueColl )
  85.         {
  86.             Console.WriteLine("Value = {0}", s);
  87.         }
  88.         // To get the keys alone, use the Keys property.
  89.         Dictionary<stringstring>.KeyCollection keyColl =
  90.             openWith.Keys;
  91.         // The elements of the KeyCollection are strongly typed
  92.         // with the type that was specified for dictionary keys.
  93.         Console.WriteLine();
  94.         foreach( string s in keyColl )
  95.         {
  96.             Console.WriteLine("Key = {0}", s);
  97.         }
  98.         // Use the Remove method to remove a key/value pair.
  99.         Console.WriteLine("\nRemove(\"doc\")");
  100.         openWith.Remove("doc");
  101.         if (!openWith.ContainsKey("doc"))
  102.         {
  103.             Console.WriteLine("Key \"doc\" is not found.");
  104.         }
  105.     }
  106. }
  107. /* This code example produces the following output:
  108. An element with Key = "txt" already exists.
  109. For key = "rtf", value = wordpad.exe.
  110. For key = "rtf", value = winword.exe.
  111. Key = "tif" is not found.
  112. Key = "tif" is not found.
  113. Value added for key = "ht": hypertrm.exe
  114. Key = txt, Value = notepad.exe
  115. Key = bmp, Value = paint.exe
  116. Key = dib, Value = paint.exe
  117. Key = rtf, Value = winword.exe
  118. Key = doc, Value = winword.exe
  119. Key = ht, Value = hypertrm.exe
  120. Value = notepad.exe
  121. Value = paint.exe
  122. Value = paint.exe
  123. Value = winword.exe
  124. Value = winword.exe
  125. Value = hypertrm.exe
  126. Key = txt
  127. Key = bmp
  128. Key = dib
  129. Key = rtf
  130. Key = doc
  131. Key = ht
  132. Remove("doc")
  133. Key "doc" is not found.
  134.  */