跳到主要內容

C# 語法 1

//enum 列舉型別
public enum ActionDescription
{
        基本資料新增  = 1, 每日資料新增 , 延遲資料新增
}

/*數字轉換為列舉型別的內容*/
public class DescriptionString
{
        public string InsPageData
        {
            get { return Convert.ToString((DataAccess.ActionDescription)1) ;}
        }
        public string InsPageDaily
        {
            get { return Convert.ToString((DataAccess.ActionDescription)2); }
        }
        public string InsPageDelay
        {
            get { return Convert.ToString((DataAccess.ActionDescription)3); }
        }
}
--------------------------------------------------------------
//字串轉換為列舉型別
public enum FileType
{
       GIF,JPG,BMP,EXE,DOC,XLS,XML
}

FileType type = (FileType)Enum.Parse(typeof(FileType), "DOC", true);
--------------------------------------------------------------
//enum 列舉型別
public enum ActionType
{
            INSERT,DELETE,UPDATE,SELECT
}

public string DoAction(ParseXml.ActionType type)
{
            string resultXml = string.Empty;
            switch (type)
            {
                case ParseXml.ActionType.INSERT:
                    resultXml = action.ActionInsert(doc);
                    break;
                case ParseXml.ActionType.DELETE:
                    resultXml = action.ActionDelete(doc);
                    break;
                case ParseXml.ActionType.UPDATE:
                    resultXml = action.ActionUpdate(doc);
                    break;
                case ParseXml.ActionType.SELECT:
                    resultXml = action.ActionSelect(doc);
                    break;
                default:
                    break;
            }
            return resultXml;
}
--------------------------------------------------------------
//Accessor
public class XmlDbSetting
{
        public static string ServerName { get; set; }
        public static string DatabaseName { get; set; }
        public static string UserName { get; set; }
        public static string Password { get; set; }
}
--------------------------------------------------------------
//Extension Method
string s = "ABC";
string s1 = s.GetExtensionsValue();  //extensions:ABC

public static class ExtensionsClass
{
        public static string GetExtensionsValue(this string value)
        {
            return string.Format("extensions:{0}", value);
        }
}
--------------------------------------------------------------
//delegate 委派  (readonly ref)
public delegate string DbConnFactory();
public readonly string DbConnString;  //const
DbConnFactory dcf;

public DataAccessBase(DbType dbType)
{
            GetDbConnString(ref DbConnString, dbType);
}

public void GetDbConnString(ref string DbConnString,DbType dbType)
{
            if (dbType == DbType.Default)
            {
                dcf = new DbConnFactory(rxs.GetDbPageMGMT);               
            }
            else if (dbType == DbType.Permission)
            {
                dcf = new DbConnFactory(rxs.GetDbPermissions);               
            }
            DbConnString = dcf();
}
--------------------------------------------------------------
//struct 結構
public struct Department
{
            public static string depName;
            public static string depId;
            public static List<string> depMembersId;

            public struct DepDetail
            {
                public static string tel;
                public static string chFullName;
                public static string enFullName;
                public static string addr;

                public struct MembersDetail
                {
                    public static Dictionary<string, Emp> membersDetail;
                }
            }
}
--------------------------------------------------------------
IUDNPageMgmt iUdnPageMgmt;
iUdnPageMgmt = new Action1();   //Action1 implement IUDNPageMgmt
iUdnPageMgmt.ActionInsert(doc);  //Action1.ActionInsert()

iUdnPageMgmt = new Action2();   //Action2 implement IUDNPageMgmt
iUdnPageMgmt.ActionInsert(doc);  //Action2.ActionInsert()
--------------------------------------------------------------
interface , abstract class , sealed class
nested class
partial class
virtual method can override by extened class
--------------------------------------------------------------
//Nullable
int? y = 0;
int? x = y ?? 1 ;    //if y is null then x = 1
--------------------------------------------------------------
@if @string
--------------------------------------------------------------
//隱含型別 var
string[] words = { "apple", "strawberry", "grape", "peach", "banana" };
var wordQuerys = from word in words where word.Contains('p') select word;
foreach (var wordQuery in wordQuerys){}
--------------------------------------------------------------
//匿名涵式
this.Button1.Click += (oSender, eArgs) =>
{
   ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('?踹?瘨萄?');", true);
};           
--------------------------------------------------------------
//params 不定個數方法參數
int r1 = UseParams();
int r2 = UseParams(0,1,2,3,4,5);

public int UseParams(params object[] list)
{
            int z = 0;
            if (list.Length == 0)
            {
                return 0;
            }
            else
            {
                foreach(int x in list)
                {
                    z += (int)x;
                }
                return z;
            }
}
--------------------------------------------------------------
out 傳址
ref 傳址  must initial object
--------------------------------------------------------------
boxing unboxing  將實值型別視為物件處理,會耗費大量運算資源的處理序
--------------------------------------------------------------
early bind 將物件指定給明確型別的變數 效能佳
last bind   將物件指定給object型別的變數
--------------------------------------------------------------
//Generic<T>   泛型
http://msdn.microsoft.com/zh-tw/library/d5x73970(v=vs.80).aspx

GenericCountByInt<int, int>(3, 5);
GenericCountByString<string>("3", "5");
GenericByClass<Employee>(new Employee() { Id = "007", Name = "Mark" });
GenericByClass<TwnEmployee>(new TwnEmployee() { Id = "008", Name = "TwnMark", Addr = "Taipei", Tel = "886" });


//泛型<實質型別: 結構 列舉型別 整數類資料型別 浮點型別 decimal bool>
public int GenericCountByInt<T,S>(T t1,S s1) where T : struct where S : struct
{
            if (t1 is Int32 && s1 is Int32)
            {
                return Convert.ToInt32(t1) + Convert.ToInt32(s1);
            }
            return 0;
}

//泛型<參考型別: class interface delegate object string>
public string GenericCountByString<T>(T t1, T t2) where T : class
{
            return Convert.ToString(t1) + Convert.ToString(t2);
}

//泛型<自訂型別>
public string GenericByClass<T>(T t1) where T : Employee
{
            if (t1 is TwnEmployee)
            {
                return (t1 as TwnEmployee).Name + "/" + (t1 as TwnEmployee).Addr + "/" + t1.ToString();
            }
            return t1.Name;
}


public class Employee
{
        public string Id { get; set; }
        public string Name { get; set; }
        public override string ToString()
        {
            return Id + " " + Name;
        }
}


public class TwnEmployee : Employee
{
        public string Tel { get; set; }
        public string Addr { get; set; }
}
--------------------------------------------------------------
//泛型程式碼中的預設關鍵字 default
string s = default(string);
預設對參考型別傳回null,對實數值型別傳回0
--------------------------------------------------------------
實值型別 (Value Type) 包含下列事項:

所有的數字資料型別
Boolean、Char 和 Date
所有結構 (即使其成員也屬於參考型別的結構)
列舉型別 (Enumeration),因為其基礎型別一定是 SByte、Short、Integer、Long、Byte、UShort、UInteger 或 ULong

參考型別 (Reference Type) 包含下列事項:

String
所有陣列 (即使其元素也屬於實值型別的陣列)
類別型別,例如 Form
委派
--------------------------------------------------------------