Skip to content

Instantly share code, notes, and snippets.

//IL,區域變數,先將Delegate宣告成null,用+=
internal class Class1
{
public delegate void MyDelegate();
public void MyTest()
{
Class1.MyDelegate mydelegate = null;
mydelegate = (Class1.MyDelegate)Delegate.Combine(mydelegate, delegate
{
Console.WriteLine("test");
-var 隱含型別
-匿名型別
-extension method
-yield return
-Enumerable<T>
-IEnumerable<T>
-Fluent Interface
-介面的方法會回傳自己這個介面,達到method chaining
-lambda
-匿名方法
class Program
{
static void Main(string[] args)
{
var list = new List<Customer>();
list.Add(new Customer() { Name = "jordan", Age = 19 });
list.Add(new Customer() { Name = "小章", Age = 20 });
list.Add(new Customer() { Name = "盧克", Age = 34 });
list.Add(new Customer() { Name = "9gy", Age = 29 });
list.Add(new Customer() { Name = "小盧克", Age = 17 });
@hatelove
hatelove / gist:2469587
Created April 23, 2012 08:32
OrderedDictionary in .NET 3.5 and .NET 4.0 throw exception
//這段程式碼,在4.0裡面會出錯,在3.5不會出錯。
class Program
{
static void Main(string[] args)
{
var o = new OrderedDictionary();
for (int i = 0; i < 3; i++)
{
o.Add(i, string.Format("{0}_{1}", i.ToString(), "yahoo"));
internal class Program
{
private static void Main(string[] args)
{
IList<CellPhone> o = new CellPhones();
o.Add(new CellPhone());
Console.WriteLine("item count:{0}", o.Count.ToString());
}
}
@hatelove
hatelove / gist:3393522
Created August 19, 2012 08:06
Refactoring with take indirection out
There is a second, rarer refactoring game.
Identify indirection that isn't paying for itself and take it out.
Often this takes the form of intermediate methods that used to serve a purpose but no longer do.
Or it could be a component that you expected to be shared or polymorphic but turned out to be used in only one place.
When you find parasitic indirection, take it out.
Again, you will have a more valuable program,
not because there is more of one of the four qualities listed earlier but
because it costs less indirection to get the same amount from the qualities.
var myCollection = this.GetMyData();
var myResult = myCollection.Select(x=> x+ ",");
var result= new StringBuilder();
foreach(var item in myResult)
{
result.Append(item) ;
}
if(len(xxx)>= 0)
{
if(xxx != "offline" && xxx != "")
{
//xxx非"offine",且xxx非""
}
else if(xxx=="offline")
{
//xxx為"offline"
}
/// <summary>
/// GrandFather 的摘要描述
/// </summary>
public class GrandFather : System.Web.UI.Page
{
protected virtual int GetExpireMinutes()
{
return 480;
}
@hatelove
hatelove / gist:4203022
Created December 4, 2012 11:53
Dynamic Proxy by 黃忠成 - 程式碼註解 by 91
// http://www.dotblogs.com.tw/code6421/archive/2012/12/03/85339.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
namespace SimpleImpl