using System;
using System.Data;
using System.Data.SqlClient;
public namespace Kehai.Exam.SourceCode //命名空間不可以有訪問修飾符 或 屬性
{
private class Program //命名空間下不能給類添加除 public internal以外的訪問修飾符
{
public void Main() //程序入口必須有 static 修飾,且 必須在返回值之前
{
SqlConnection connection = null;
try
{
connection = new SqlConnection("連接字符串略"); //未打開連接
SqlCommand command = new SqlCommand(
"SELECT name FROM sysdatabases",
connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.HasRows)
{
string name = reader["name"]; //reader 返回的是Object類型要轉換
Console.WriteLine(name);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if(reader != null) //超出了reader 的作用域
reader.Close(); //超出了reader 的作用域
if (connection != null) //超出了connection 的作用域
connection.Close(); //超出了connection 的作用域
}
}
}
}
二
ArrayList arrayList = new ArrayList();
arrayList.Add(1);
arrayList.Add(2);
arrayList.Add(3);
arrayList.Add(4);
arrayList.Add(5);
//刪除對象時,索引從 0 開始
arrayList.RemoveAt(3); //每次刪除一個,arraryList就重新排序, 索引值就會發生
arrayList.RemoveAt(4); //改變。可能會發生,索引越界的錯誤!
arrayList.RemoveAt(5);
三
ArrayList和List<T>都提供了Remove方法
Remove方法使用對象的Equals方法確定匹配,並刪除第一個匹配項
四
//往集合裏面添加元素時,他們在集合中的順序會按照鍵值的降序排列
using System;
using System.Collections;
namespace Kehai.Exam.SourceCode
{
public class MyClass
{
static void Main()
{
Hashtable hashTable = new Hashtable();
hashTable.Add(5, "張三");
hashTable.Add(3, "李四");
hashTable.Add(4, "王五");
hashTable.Add(1, "陳六");
hashTable.Add(2, "趙七");
foreach (object value in hashTable.Values)
Console.WriteLine(value);
}
}
}
五
Try catch finally
Try 後面必須跟一個catch 或 finally 且 finally 裏不可以 return ,不可離開finally子句,finally 裏的所有語句必須被執行
六
抽象類裏面可以有抽象方法,也可以有實例方法,接口裏只能所有的成員都必須是未實現的,且接口裏的成員不能有訪問修飾符修飾,不能有字段