C# 新语法收集

内联变量

使用int.tryparst时,先要申明变量,用于out参数

int d;

int.tryparse(s,out d);

使用内联变量写法可以如下.功能一样简化了写化

int.tryparse(s,out int d)

$"{val1}-{val2}"

作用如同string.format("{0}-{1}",val1,val2) , $开头的字符串中的{}内将当做变量解析.

方法内写方法

可以在方法内部写一些方法.类似JS的这种在函数中写一个函数

  function showmsg(msg)

       {

    function validate(){}

  }

/*

比如处理一个数字字符串排序,如果只是临时用一下,可以不必再加个方法,直接在方法内写个处理方法就行了

*/

  // 将一个数字组成的串排序

  private string orderlist(string strnum)

  {

  }

  // 给定字符串,返回排序后结果

  public string orderstringnum(string str,int ordertype)

  {

    return this.orderlist(str);

  }

// 直接写在方法内

  // 给定字符串,返回排序后结果

  public string orderstringnum(string str,int ordertype)

  {

    string orderlist(string strnum)

    {

    }

    return orderlist(str);

  }

// 注意事项:方法前面不能加private public之类,它只能在声明方法内部使用.和LAMBDA的作用相似,但是它本质是个方法,并不会像LAMBDA那样写起来别扭.

猜你喜欢

转载自www.cnblogs.com/mirrortom/p/9249941.html