Why would var be a bad thing?

前端 未结 17 1904
误落风尘
误落风尘 2020-12-04 17:21

I\'ve been chatting with my colleagues the other day and heard that their coding standard explicitly forbids them to use the var keyword in C#. They had no idea

17条回答
  •  时光取名叫无心
    2020-12-04 18:08

    Here are the results of a test I ran on efficiency of var versus explicit typing:

      private void btnVar_Click(object sender, EventArgs e)
        {
            Stopwatch obj = new Stopwatch();
            obj.Start();
            var test = "Test";
            test.GetType();
            obj.Stop();
            lblResults.Text = obj.Elapsed.ToString();
        }
    
        private void btnString_Click(object sender, EventArgs e)
        {
            Stopwatch obj = new Stopwatch();
            obj.Start();
            string test = "Test";
            obj.Stop();
            lblResults.Text = obj.Elapsed.ToString();
    
        }
    

    First Label result is: 00:00:00 000034

    Second Label result is: 00:00:00 00008

提交回复
热议问题