npm proxy設定

2017年10月24日 星期二

因為公司電腦要透過proxy才能出去,在使用npm套件管理時若沒設定proxy會出現407 Proxy Authentication Required錯誤訊息


我們可以開啟這檔案c:\users\xxx\.npmrc


輸入以下設定即可

https-proxy=http://username:password@proxy.xxxx.com:port/
proxy=http://username:password@proxy.xxxx.com:port

Read more...

Lazy 性能優化,實現延遲初始化

2017年4月13日 星期四

.Net 4.0中推出Lazy來實現延遲初始化以達到性能優化的目的
例如:建立某個物件時需要很多資源,但是在系統運行中不一定會把上用到
這時就可透過延遲初始化來達到目的,可以提高程式效率以更節省資源。

以下透過範例來說明:




執行結果

由上範例可以知道,剛開始時並沒有執行初始化,而是到car.Value.Name時才真正去載入。




參考來源:http://www.cnblogs.com/yunfeifei/p/3907726.html

Read more...

設定自動與內部主機時間同步,使用net time指令

2017年2月12日 星期日

當公司有些主機無法對外連線,所以無法使用與外部同步的服務,可以使用此語法,讓內部主機可以同步

例:
可對外主機 192.168.1.11 (A機器)
不同對外主機 192.168.1.12(B機器)

此時可在B機器操作以下方式
1.開啟命令提示字元
2.使用net use 指令跟A機器建立連線 => net use \\192.168.1.11
3.輸入使用者名稱與密碼
4執行net time \\192.168.1.11 /set /y


也可寫成批次檔,如下內容:
net use \\IP或主機名稱 密碼 /user:使用者名稱 --->連線對時主機
net time \\IP或主機名稱 /set /y --->校正時間
net use \\IP或主機名稱 /del --->中斷連線




參考來源:http://ithelp.ithome.com.tw/articles/10174201

Read more...

yield

2017年1月9日 星期一

yield,它可以讓程式員以傳回每個元素的方式來自動產生IEnumerable<T>物件

例如下列程式會回傳1-5 五個數字的IEnumerable<T>物件:

這是一般的寫法,因為List<T>有實作IEnumerable<T>,所以很自然的會用List<T>作為產製IEnumerable<T>的寫法。

        static IEnumerable<int> GetCollection1()
        {
            List<int> list = new List<int>();
            for (int i = 1; i <= 5; i++)
            {
                list.Add(i);
            }
            return list;
        }

改為yield寫法,看起來更為精簡,yield指令會告訴編譯器,這一段函式的回傳值IEnumerable<T>內的元素由yield return所回傳的物件來填充,因此可省下額外使用集合物件事先封裝的工作。

  
        static IEnumerable<int> GetCollection2()
        {
            for (int i = 1; i <= 5; i++)
            {
                yield return i;
            }
        }

 

比較二個方式的效能

    private static int testMax = 5000000;
    static void Main(string[] args)
    {

        Stopwatch sw = new Stopwatch();
        sw.Start();
        var result = GetCollection1();
        sw.Stop();
        Console.WriteLine("Collection1:{0},count:{1}", sw.ElapsedMilliseconds, result.Count());

        sw.Restart();
        var result2 = GetCollection2();
        sw.Stop();
        Console.WriteLine("Collection2:{0},count:{1}", sw.ElapsedMilliseconds, result2.Count());

        sw.Restart();
        var result4 = GetCollection4();
        sw.Stop();
        Console.WriteLine("Collection4:{0},count:{1}", sw.ElapsedMilliseconds, result4.Count());

	sw.Restart();
        var result5 = GetCollection5();
        sw.Stop();
        Console.WriteLine("Collection5:{0},count:{1}", sw.ElapsedMilliseconds, result5.Count());

    }

    static IEnumerable<int> GetCollection1()
    {
        List<int> list = new List<int>();
        for (int i = 1; i <= testMax; i++)
        {
            list.Add(i);
        }
        return list;
    }

    static IEnumerable<int> GetCollection2()
    {
        for (int i = 1; i <= testMax; i++)
        {
            yield return i;
        }
    }
    
    static IEnumerable<Student> GetCollection4()
    {
        for (int i = 1; i <= testMax; i++)
        {
            yield return  new Student()
            {
                Id = i,
                Name = i.ToString()
            };
        }
    }

    static IEnumerable<Student> GetCollection5()
    {

        List<Student> studentList = new List<Student>();
        for (int i = 1; i <= testMax; i++)
        {
            studentList.Add(new Student() {Id=i,Name=i.ToString() });
        }
        return studentList;
    }
    
    public class Student
    {
        public int Id { get; set; }

        public string Name { get; set; }
    }

 

 

 

執行結果,我們發現使用yield寫法,所執行時間為0,這是因為延遲查詢,當我們對這個集合查詢時才會真正載入資料。

image

 

//查詢階段才會真正載入資料
Console.WriteLine(result4.Where(o => o.Id == 700).Select(o => o.Name).FirstOrDefault());

 

若是在foreach這類迭代運算中要中斷執行,則可利用yield break來中斷,如下程式碼:

    static IEnumerable<int> GetCollection3(IEnumerable<int> NumberSeries)
    {
        foreach (var number in NumberSeries)
        {
            if (number > 10)
            {
                yield break;
            }
            else
            {
                yield return number;
            }
        }
    }

執行結果

image

Read more...

C# 6.0 筆記

2016年12月7日 星期三

C# 6.0使用roslyn編譯器,而roslyn是被內建在VS2015,雖然VS2013可透過Nuget安裝”Microsoft.Net.Compilers”來使用C#6.0,但是不知道會不會有未知問題,所以還是建議使用VS2015來寫比較好。

 

1.Auto property enhancements (自動屬性初始化)

    class Student
    {
        public string Name { get; set; } = "Neil";

        public string Id { get; set; } = "A001";

        public DateTime DateCreated { get; private set; } = DateTime.Now;
    }

 

2.Expression-bodied funtion(成員使用lambda運算式)

    class Student
    {
        public string Name { get; set; } = "Neil";

        public string Id { get; set; } = "A001";

        public DateTime DateCreated { get; private set; } = DateTime.Now;

        //C# 6.0新增寫法
        public string GetStudentInfoNew => string.Format("Name:{0},Id:{1}", Name, Id);

        //一般寫法
        public string GetStudentInfoOld()
        {
            return string.Format("Name:{0},Id:{1}", Name, Id);
        }
    }

 

3.Using Static(使用Using 靜態命合類別)

using static System.String;//使用using static將string載入

namespace Csharp567
{
    public class Sample
    {
        public bool CheckIsNullNew(string val)
        {
            //不用string就可以直接使用IsNullOrEmpty方法
            return IsNullOrEmpty(val);
        }

        public bool CheckIsNullOld(string val)
        {
            return string.IsNullOrEmpty(val);
        }
    }
}

 

4.Null conditional operators(Null條件運算子)

public void GetStudentName()
{
    List<Student> students = null;
    students = new List<Student>() { new Student() { Name = "Neil", Id = "001" } };

    //新寫法
    if (students?[0]?.Name != null)
    {
        Console.WriteLine(string.Format("Name:{0}", students[0].Name));
    }

    //舊寫法
    if (students != null && students[0] != null && students[0].Name != null)
    {
        Console.WriteLine(string.Format("Name:{0}", students[0].Name));
    }
}

 

5.String interpolation(字串插補)

public void ShowString()
{
    string name = "Neil";
    int id = 1;
    //舊寫法
    string oldStr= string.Format("Name:{0},Id:{1}", Name, Id);
    //新寫法
    string newStr = $"Name:{Name},Id:{Id}";
}

 

 

6.nameof expressions(nameof運算子)

public void ShowStudentId()
{
    int studentId = 123;
    //用vs工具改名稱會一起更改
    string str = $"MethodName:{nameof(ShowStudentId)} ,{nameof(studentId)}:{studentId} Show Success!!";
    //result => ShowStudentId , studentId:123 Show Success!!
}

 

7.Index initializers(索引初始設定)

var studentsOld = new Dictionary<int, string>
{
    {1,"Neil" },
    {2,"Jeremy" }
};

var studentNew = new Dictionary<int, string>
{
    [1] = "Neil",
    [2] = "Jeremy"
};

 

8.Exception filters(例外過濾)

try
{
    var r = 0;
    r = 0 / 0;
}
catch (Exception ex) when (ex.Message.Contains("KeyeError")) //可filter自訂義條件
{

    Console.WriteLine($"msg: {ex.Message}");
}

Read more...

const vs readonly

2016年11月26日 星期六

 

1.const的常數是在編譯時期將所有使用到的地方替換成實際的數值(效能較好)

 

2.readonly是在實際執行期間才會去查找(靈活性高),可當做執行階段當數使用

例:public static readonly uint timeStamp = (uint)DateTime.Now.Ticks;

 

3.const的常數不能同時是static

 

4.readonly不能在method中使用

 

5.readonly可以在建構式裡給初始值

 

 

2016-11-26 下午 10-35-12

 

利用ILSpy反編譯工具查看編譯出來的dll

2016-11-26 下午 10-35-50

 

 

 

public class ReadOnlyTest
  {
     class SampleClass
     {
        public int x;
        // Initialize a readonly field
        public readonly int y = 25;
        public readonly int z;

        public SampleClass()
        {
           // Initialize a readonly instance field
           z = 24;
        }

        public SampleClass(int p1, int p2, int p3)
        {
           x = p1;
           y = p2;
           z = p3;
        }
     }

     static void Main()
     {
        SampleClass p1 = new SampleClass(11, 21, 32);   // OK
        Console.WriteLine("p1: x={0}, y={1}, z={2}", p1.x, p1.y, p1.z);
        SampleClass p2 = new SampleClass();
        p2.x = 55;   // OK
	//p2.y = 10; //因為不是在建構函數給值所以會出錯
	//錯誤訊息:無法指定唯讀欄位(除非在建構函數或變數初始設定式)
        Console.WriteLine("p2: x={0}, y={1}, z={2}", p2.x, p2.y, p2.z);
     }
  }
  /*
   Output:
      p1: x=11, y=21, z=32
      p2: x=55, y=25, z=24
  */

 

 

參考來源:

https://dotblogs.com.tw/yc421206/archive/2011/06/06/27232.aspx

http://slmtsite.blogspot.tw/2016/10/c-36-constant.html

https://msdn.microsoft.com/zh-tw/library/e6w8fe1b.aspx

https://msdn.microsoft.com/zh-tw/library/acdd6hb7.aspx

Read more...

String Pool

2016年11月22日 星期二

 

string pool簡單說就是一個Hash Table ,其中Key值是字串內容,Value是物件實體位置。

內容值會在程式編譯時期做初始設定,將程式碼中用到的用串加入,所以不同字串變數存放的靜態字串,

若是一樣的字串,字串會在編輯時期加入String Pool,透過String Pool的協助兩個變數會指到相同的物件實體。

 

一般的字串比對底層是以Byte為基礎的方式去比對,當比對的字串很長時,整個處理效能就跟著低落,若是可以善用

string pool,我們只需比對兩者是否指到相同的物件實體就可以了,可以獲得較佳的效能。

參考Larry大的文章,用ReferenceEquals效能是較好的。

 

2016-11-22 下午 11-29-52

執行結果

2016-11-22 下午 11-30-24

 

2016-11-22 下午 11-31-26

執行結果

2016-11-22 下午 11-32-18

2016-11-22 下午 11-34-13

執行結果

2016-11-22 下午 11-34-25

 

 

 

參考:https://dotblogs.com.tw/larrynung/archive/2011/06/30/30763.aspx

Read more...