目前分類:C# 技術文章 (22)
- Nov 29 Thu 2012 17:37
[C#] HTML Agility Pack 相關
- Nov 19 Mon 2012 18:40
[C#] Convert Json to C# DataStruct by JSON.NET library
JSON.NET Sample
JSON deserialization with JSON.net
To help user to auto-convert json to c# data structure.
json2csharp
download json.net lib and document.
json.net library
- Oct 11 Thu 2012 18:05
C# 與 ReportingService2005 互動的方法
Step1:開啟一個專案(EX: console application)
Step2:如下圖操作(點選 Reference),選取 Add Web Service,會跳出一個視窗。該視窗是WCF設定視窗,不過我們需要用的是舊型的Web Service,所以點選左下角的按鈕(Advance)。
Step3: 繼續往舊版的Web Service設定前進(點選下方的按鈕)
- Sep 17 Mon 2012 18:42
[C#] C# 讀取 Excel file 相關資料
Tutorial
http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm
The Simple method of use
http://www.dotblogs.com.tw/yc421206/archive/2009/01/11/6727.aspx
- Jul 13 Fri 2012 00:25
[C#] ref 關鍵字利用在 object上的效果。
睡前翻書翻到的。然後上msdn發現...嘿嘿,竟然沒講。
http://msdn.microsoft.com/zh-tw/library/14akc2c7(v=vs.100).aspx
這有點難用程式碼寫出來,不過我覺得應該用個...一些圖表示,有空在畫好了。
// 預備畫圖...
這個關鍵字簡單來說就跟pointer概念上是一樣的,但是.....在C#裡面,假設你是用Object的話,他本身在傳遞上就是Call by reference。
- Jun 27 Wed 2012 00:21
[C#] C# 4.0:Named and Optional Arguments
http://msdn.microsoft.com/en-us/library/dd264739.aspx
msdn的說明。
先講 Optional Arguments(可選參數)應用在Method(方法)上,這功能就是幫你預設好參數(Arguments)值,當有輸入值的時候就聽遵循輸入值的話 ; 當沒有輸入值的時候就直接把預測值塞入。 這東西可以用Overload的寫法實作,但是程式碼將會壟長很多..。
(程式碼實作比較:Optional Arguments vs overload 寫法)
- Jun 19 Tue 2012 20:00
[C#] .Net 4.0 新功能 ZIP!!!
當初是為了處理兩個Array / List 放在同一個For loop內一起使用,但是不想要改動到既有的程式碼而上網查的。(簡單來說本來功能都寫好了,但是我臨時發現忘記記錄一筆資料,但我List 的 Data struct都設計好了不想再更動他,所以想想有沒有比較偷懶的方法)
.Net 4.0推出這個東西其實還蠻不錯的.....雖然當下看會覺得這個Func真的是一點屁用都沒有,因為一般人不會那麼蠢把都要放到For loop中的資料拆成兩個Array。
不過站在這種奇妙的案例下這Func就蠻有用的...= =++
http://stackoverflow.com/questions/4450650/using-foreach-loop-to-iterate-through-two-lists
- Jun 15 Fri 2012 15:10
[C#] 比較兩個 List<int> 是否相同的方法
- Jun 14 Thu 2012 18:29
[C#] 實現Singleton的方法比較
- Jun 12 Tue 2012 15:57
[C#] "Where" of LINQ (three methods)
- Apr 15 Sun 2012 16:38
[C#] "Where" of LINQ (three methods)
// Main function
class MainFunc { static void Main(string[] args) { string[] animals = new string[] { "Koala", "Spider", "Elephant" }; wherepractice prac = new wherepractice(); // get animals of StartName is "S" prac.detailWhere(animals); // get amimals of StartName is "E" prac.lambdaWhere(animals); // as above result, but different implement. prac.delegateImpleWhere(animals); Console.ReadLine(); return; } }
// subClass
public void detailWhere(string[] animals) { var q = from a in animals where a.StartsWith("S") select a; foreach (string s in q) { printString(s); } } public void lambdaWhere(string[] animals) { var q = animals.Where(a => a.StartsWith("E")); foreach (string s in q) { printString(s); } } public void delegateImpleWhere(string[] animals) { // To utilize "delegate" to implement lambda expression "Where". var q = animals.Where( delegate(string a){return a.StartsWith("E");} ); foreach (string s in q) { printString(s); } }
- Apr 15 Sun 2012 16:30
[C#] goto statement
This is a simple sample for Exit statement.
static void Main(string[] args) { Program.correctGoto(); Program.GotoInfiniteLoop(); Program.falseGoto(); } //This is a correct Goto statement method. static public void correctGoto() { for (int i = 0; i < 100; i++) { Console.WriteLine("{0}", i); if (i == 5) { goto Exit; } } Exit: Console.WriteLine("Exit"); } //If you put Exit tag before goto statement, it will cause infinite loop.p> static public void GotoInfiniteLoop() { Exit: Console.WriteLine("Exit"); for (int i = 0; i < 100; i++) { Console.WriteLine("{0}", i); if (i == 7) { goto Exit; } } } //It's a wrong method to use exit statement. static public void falseGoto() { for (int i = 0; i < 100; i++) { Console.WriteLine("{0}", i); if (i == 7) { Exit: Console.WriteLine("Exit"); } // this method can't jump from external into internal. goto Exit; } }
- Apr 12 Thu 2012 14:09
[C#] "Where" of LINQ (three methods)
// Main function
class MainFunc { static void Main(string[] args) { string[] animals = new string[] { "Koala", "Spider", "Elephant" }; wherepractice prac = new wherepractice(); // get animals of StartName is "S" prac.detailWhere(animals); // get amimals of StartName is "E" prac.lambdaWhere(animals); // as above result, but different implement. prac.delegateImpleWhere(animals); Console.ReadLine(); return; } }
// subClass
public void detailWhere(string[] animals) { var q = from a in animals where a.StartsWith("S") select a; foreach (string s in q) { printString(s); } } public void lambdaWhere(string[] animals) { var q = animals.Where(a => a.StartsWith("E")); foreach (string s in q) { printString(s); } } public void delegateImpleWhere(string[] animals) { // To utilize "delegate" to implement lambda expression "Where". var q = animals.Where( delegate(string a){return a.StartsWith("E");} ); foreach (string s in q) { printString(s); } }
- Apr 10 Tue 2012 20:14
[C#] List Initialization between 2.0 and 3.0
class ListInit { static void Main(string[] args) { // The demo of List Initialize Set including old declaration and new declaration(C#3.0) // Old initialization. List stringOld = new List(); stringOld.Add("string_1"); stringOld.Add("string_2"); // New initialization. List stringNew = new List() { "string_1", "string_2" }; // Combine Object and List Init. List list = new List{ new Contact{ LastName = "Alen", DateOfBirth = new DateTime(1234,1,13) }, new Contact{ LastName = "AlenTwo", DateOfBirth = new DateTime(2345,1,23) } }; } private class Contact { public string LastName { get; set; } public DateTime DateOfBirth { get; set; } } }
- Apr 06 Fri 2012 13:40
[C#] Object Initialization between 2.0 and 3.0
class Program { static void Main(string[] args) { // The demo of Object Initialize Set including old declaration and new declaration(C#3.0) // If member access modifier isn't declared as "public", method_1 and method_2 isn't working. // method_1 Contact contact = new Contact(); contact.LastName = "A-len"; contact.DateOfBirth = new DateTime(2012, 04, 03); //method_2 Contact contactNew = new Contact { LastName = "A-len", DateOfBirth = new DateTime(2012, 04, 03) }; } private class Contact { public string LastName { get; set; } public DateTime DateOfBirth { get; set; } } }
- Jan 19 Thu 2012 00:19
[C#]How to adjust the encoding in StreamReader
StreamReader defaults to UTF-8 encoding unless specified otherwise, instead
of defaulting to the ANSI code page for the current system.
(Hint: Convert xls/xlsx file to csv file. the encoding is ANSI.)
http://msdn.microsoft.com/zh-tw/library/6aetdk20(v=vs.100).aspx
- Jan 15 Sun 2012 22:35
[C# 3.0] Type Parameter Assumption
Main Function
namespace ConsoleGenericsAssumption { class Program { static void Main(string[] args) { GenericTypeResolverTest v = new GenericTypeResolverTest("Hello Alam"); //v.Value = "Hello ALam"; v.ShowInConsole(); // -----same result, differnet implementation--- Console.WriteLine("-----same result, differnet implementation---"); string showString = v.ToString(); Console.WriteLine("{0}", showString); Console.ReadLine(); // generic for string string ValueString = "Hello Yaya"; ValueString.ShowInConsole(); // generic for int int ValueInt = 12345; ValueInt.ShowInConsole(); } } }
Another class
public class GenericTypeResolverTest { public string Value { get; set; } public GenericTypeResolverTest() { } public GenericTypeResolverTest(string value) { Value = value; } public override string ToString() { return Value.ToString(); } } public static class GenericTypeResolverMethodTest { public static void ShowInConsole(this T obj) { Console.WriteLine(obj.ToString()); Console.ReadLine(); } }
- Jan 09 Mon 2012 00:54
[C#] What's different of [], List and ArrayList.
static void Main(string[] args) { // [] is specific type and fixed length. // It is a data structure. String[] arr = new string[] {"Hi", "I", "am", "ALam"}; ShowMessage(arr); // List is specific type and unbounded length. // namespace is System.Collection.Generic List list = new List(); list.Add("Hi"); list.Add("I"); list.Add("am"); list.Add("Alam"); ShowMessage(list); // ArrayList is any type and undounded length. // namespace is System.Collection ArrayList arraylist = new ArrayList(); arraylist.Add("Hi"); arraylist.Add("I"); arraylist.Add("am"); arraylist.Add("Alam"); arraylist.Add(12345); // when we call the ShowMessage, it will be wrong. ShowMessage(arraylist); } private static void ShowMessage(string[] temp) { foreach (string aaa in temp) { Console.WriteLine("{0}", aaa); } Console.ReadLine(); } private static void ShowMessage(List temp) { foreach (string aaa in temp) { Console.WriteLine("{0}", aaa); } Console.ReadLine(); } private static void ShowMessage(ArrayList temp) { foreach (string aaa in temp) { Console.WriteLine("{0}", aaa); } Console.ReadLine(); } }
- Jan 08 Sun 2012 23:29
[C#] access stored procedure by linq
// to associates a stored procedure which user defined in the database. // Must write it before func. [global::System.Data.Linq.Mapping.FunctionAttribute(Name ="stored procedure name")] // Undertake on a code, we make a func. let user to access the stored procedure. // When SP exist input variables, you must use "Linq.Mapping.ParameterAttribute" to // select the variable in DB and use "System.Nullable" to set // what variable you wnat to assign in code. public int storedProcedureForC#Class( [global::System.Data.Linq.Mapping.ParameterAttribute(Name:"inputVarName", DbType="inputVarType"] System.Nullable varInC#) { // Execute the stored procedure. IExecuteResult objectForSP = this.ExecuteMethodCall(this, ( (MethodInfo)(MethodInfo.GetCurrentMethod())), varInC#); return ((int)(result.ReturnValue)); }
- Jan 04 Wed 2012 22:55
[C#] Specific Symbol"as" is used in Session of asp.net
So........We know "as" is a powerful method that perform certain types of conversions between compatible reference types. for example:
// ...... // Array[] = dataTable; // ....do something for dataTable.// const string = "ALamTest"; Session[ALamTest] = dataTable; //....do another something...// Array[] getTable = Session[AlamTest] as Array[]
When we use Session Mechanism, "as" is a useful method let us to redesign the object which is taken out from the session.