【c#基础知识大盲扫1】

1拉姆达表达式的使用

//拉姆达输出集合值
//时间:2013-1-2 13:54:40;
//作者:白宁超
 List<string> list = new List<string> { "hello", "luo", "fei" };
 list.ForEach(a => Console.WriteLine(a));

 a表示参数,利用list.ForEach可以更加明了化,体现拉姆达表达式的简洁性。


2比较等值问题

View Code
1             //比较等值问题
2             //时间:2013-1-2 13:54:40;
3             //作者:白宁超
4             StringBuilder c = new StringBuilder("AAA");
5             StringBuilder d = new StringBuilder("AAA");
6             Console.WriteLine(c == d); //false          
7             Console.WriteLine(c.Equals(d));//true
8             Console.ReadLine();

==返回两个对象的比较,而Equals是对象返回字符串的比较


3   //简化属性
    //时间:2013-1-2 13:54:40;
    //作者:白宁超

class Class1
    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        //public string name { get; set; }//与上面效果一样      
    }

 main函数:

        static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            //c1.Name = "白宁超";//输出白宁超
            c1.Name = "张三";//输出张三
            Console.WriteLine(c1.Name);
        }

 public string name { get; set; }具体get和set属于托管运行的


4   ////操作委托
    ////时间:2013-1-2 13:54:40;
    ////作者:白宁超

 class Class1
    {
        public delegate void something(int a); //定义方法委托
        public void DoIt(int a)//构造参数化方法
        {
            Console.WriteLine(a);
        }
        public void HowtoDo(something doMethod, int a)//构造委托方法和参数
        {
            doMethod(a);
        }   
    }
 static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            c1.HowtoDo((c1.DoIt), 10);//10
            int x = 10;
            //使用匿名委托
            c1.HowtoDo(delegate(int a)
            {
                Console.WriteLine(a + x);//20
            }, 10);
            //使用lamda表达式
            c1.HowtoDo(a => Console.WriteLine(a + x), 10);//20
            c1.HowtoDo((c1.DoIt),10);//20
            c1.HowtoDo(delegate(int a)
            {
                Console.WriteLine(a);//20
            }, 10);
            Console.ReadKey();
        }

 定义委托后,可以构造多类型的方法,调用必须调用带委托委托方法的方法。也可与拉姆达配合使用。委托在处理事务上灵活性更好


5      //using==try finally
        //时间:2013-1-2 13:54:40;
        //作者:白宁超

 static void Main(string[] args)
        {
            StreamWriter sw = null;
            //对文件的写操作
            try
            {
                FileStream steam = new FileStream(@"D:\123.txt", FileMode.Create);
                sw = new StreamWriter(steam);
                Console.WriteLine("留言板:");
                string liuyan = Console.ReadLine();////abc
                sw.WriteLine(liuyan);
                sw.WriteLine("留言完成!");
            }
            finally
            {
                if (sw != null) sw.Dispose();
            }

            //对文件的读操作
            try
            {
                FileStream steam = new FileStream(@"D:\abc.txt", FileMode.Open);
                StreamReader reader = new StreamReader(steam, Encoding.GetEncoding("UTF-8"));

                string str = reader.ReadToEnd();
                Console.WriteLine(str);//显示abc 留言完成  读取结束
                Console.WriteLine("读取结束!");
                reader.Close();
                steam.Close();
            }
            finally
            {

            }
        }

 下面使用using执行后与try。。。finally效果一样

 static void Main(string[] args)
        {
            //对文件写操作
            using (var sw = new StreamWriter(@"D:\abc.txt"))
            {
                Console.WriteLine("留言板:");
                string liuyan = Console.ReadLine();
                sw.WriteLine(liuyan);
                sw.WriteLine("留言完成!");
            }
            //对文件读操作
            using (var rd = new StreamReader(@"D:\abc.txt"))
            {
                string str = rd.ReadToEnd();
                Console.WriteLine(str);
                Console.WriteLine("读取完毕");
            }
            Console.ReadKey();
        }

 using具有自动关闭释放资源的作用,在数据库操作时也是经常使用的


 6////类型实例化语法
    ////时间:2013-1-2 13:54:40;
    ////作者:白宁超

    class Class1
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
 static void Main(string[] args)
        {
            var c1 = new Class1
            {
                ID = 1,
                Name = "bainingchao",
                Age = 22,
            };
            Console.WriteLine("{0}\n{1}\n{2}", c1.ID, c1.Name, c1.Age);
            Console.ReadKey();
        }

7//扩展方法正则验证是否数字
  //时间:2013-1-2 13:54:40;
  //作者:白宁超

 //正则表达式验证是否输入数字
    public  class StringExt
    {
        private Regex regexNumber = new Regex("\\d+");
        public bool IsNumber(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return false;
            }
            return regexNumber.IsMatch(input);
        }
    }
       static void Main(string[] args)
        {
            StringExt se = new StringExt();
            string input = Console.ReadLine();
            Console.WriteLine(se.IsNumber(input));
            Console.ReadLine();
        }

 Regex表示不可变的正则表达式


        static void Main(string[] args)
        {
            string str1 = "789";
            string str2 = "789";
            Console.WriteLine(Object.ReferenceEquals(str1, str2));//确定是否具有相同的实例true
            string str3 = "7" + "8" + "9";
            Console.WriteLine(Object.ReferenceEquals(str1, str3));//true
            char[] chars = new char[] { '7', '8', '9' };
            string str4 = new string(chars);
            Console.WriteLine(object.ReferenceEquals(str1, str4));//false
            Console.ReadLine();
        }
原文地址:https://www.cnblogs.com/baiboy/p/2844592.html