.net语法

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace a
{
   public class Program
    {
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();
            //匿名委托
            mc.HowToDoIt(delegate(int a) {
                Console.WriteLine(a);
            },10);
            //拉姆达表达式
            mc.HowToDoIt(a => Console.WriteLine(a), 10);
           
            //将数据循环出来
            List<string> strlist = new List<string> { "aa", "bb" };
            strlist.ForEach(a => Console.WriteLine(a));//.net语法糖
            foreach (var item in strlist)//普通写法
            {
                Console.WriteLine(item);
            }


            //将文件写入磁盘中(普通写法)
            StreamWriter sw = null;
            try
            {
                sw = new StreamWriter(@"d:\abcd.txt");
                sw.WriteLine("test");
            }
            finally
            {
                if (sw != null) sw.Dispose();
            }
            //讲文件写入磁盘(.net语法糖写法)
            using (var sws=new StreamWriter(@"d:\abs.txt"))
            {
                sws.WriteLine("test");
            }
            //读取文件内容
            using (var sr=new StreamReader(@"d:\abs.txt"))
            {
                Console.WriteLine(sr.ReadLine()); 
            }
           // 三元表达式
            var b = 3;
            var c = b > 9?b.ToString():"0"+b;
            Console.WriteLine(c);
            //两个问号表示,如果左边的是空的话,就等于右边的值,如果右边是空的话就等于左边的值
            string aa = "我是aa";
            string bb = aa ?? "我是bb";
            Console.WriteLine(bb);


            //测试传入的数值是否是数字
            var isnum = TestNumber.IsNumber("123");
            Console.WriteLine(isnum);


            //匿名类
            var li = new
            {
                ID="11",name="小红",age=21
            };
            Console.WriteLine("我是"+li.name+",今年"+li.age+"岁。");


            Console.ReadLine();
        }
      
    }

}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace a
{
   public class MyClass
    {

//声明一个委托
       public delegate void DoSomeThing(int a);

       public void HowToDoIt(DoSomeThing doMethod,int a) {
           doMethod(a);
       }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;


namespace a
{
  public static class TestNumber
    {
      static private Regex tSnumber = new Regex("\\d+");//实例化一个正则表达式
      //将传人的参数与正则表达式匹配
      static public bool IsNumber(this string number)
      {
          if (string.IsNullOrEmpty(number))
          {
              return false;
          }
          else
          {
              return tSnumber.IsMatch(number);
          }
      }
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值