写漂亮C#代码的小技巧_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 写漂亮C#代码的小技巧

写漂亮C#代码的小技巧

 2016/8/7 5:30:34  张日意  程序员俱乐部  我要评论(0)
  • 摘要:第一次写博客,不知道代码用什么编辑,直接截图了,哈哈哈。。。。我自己不喜欢看随便复制粘贴过来一堆代码的博客,所以,用些简单点的例子吧,希望对大家有帮助。。。一、自动属性。1、vs下输入prop,Tab键就出现了。2、有了自动属性,我们不用再额外为一个类的每个公共属性定义一个私有字段(实际上没多大用处的字段),但是通过反射还是可以看到对应的私有字段的。例:二、初始化器。假如创建了一个list对象,并要添加一些元素。1、一般做法是:2.利用初始化器:三、双问号与三目运算符。如果list为null
  • 标签:C# 代码 技巧

  第一次写博客,不知道代码用什么编辑,直接截图了,哈哈哈。。。

  我自己不喜欢看随便复制粘贴过来一堆代码的博客,所以,用些简单点的例子吧,希望对大家有帮助。。。

一 、自动属性。

  1、vs下输入prop,Tab键就出现了。

    2、有了自动属性,我们不用再额外为一个类的每个公共属性定义一个私有字段(实际上没多大用处的字段),

    但是通过反射还是可以看到对应的私有字段的。

  例:

   

二、初始化器。

  假如创建了一个list对象,并要添加一些元素。

  1、一般做法是:

   QQnMsc72jt49UGy+nMtD6ovpvtrhWuTYEjXcStJez83/zdvrE7PX4PEbtPkB271fK1avEzdwc72juXWRRWB9XBc7trH//i95b4SYapObGt1uweu9Xy5+whYvcJOsdueyfee2Q3hX+nPtsh5PErGX54kqFcrhZOscyd7CE79xBXMtxet+t2y2OrdmG1nfoqFldlduyo+vG1Var2Ms0G1Xth1zy3nIRuFlYbCfa4tT71383Ktz6F01bs2kOmnXtIMHbF8XXZcnA68z+3m3W4HFsOIJXYBUgldgFSiV2AVGIXIJXY5aQiv+bDFfW5XWLtmsrGVe7BNhtXa3Z0+4F9uW3klgFurNvefGTgHbk1aJBbjvMLbSOxy42J3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JXYBUgldgFSid2fcaoUnsnI/xmxC8/00JE/NPKCjYtdeKae1+1W3zrefrDNxqW+my10X89Ig2IXnmngXWqDPtuoHIz+hEwUu8Cai8Xu/E6zYDvlnWZ9V+nrFsQuPNOo2F0787C4ubZ6u+1mL417dtuFkfVsrNKisL3ym38DxC4809jZbiPyvgugMnary43Gvz4R0eho7Y9B86uIXXio1Nidwr9xNXopJ6fl8qDYLee2YhfYKzt2q+XBAIon7OjZ7qJQ7AJxJz3JsDkp/jp2u5RsznYjkSp24Zk6jPy1o+92YbWRtf/OP7tY3rUCn7faKx9Zh3mF+VqVXTS6rtYB7u2hI39o5DUaF7uAkf8zYheeycj/GbELz2TkA6QSuwCpxC5AKrELkErsAqQSuwCpxC5AKrELkErsAqQSuwCpxC5AKrELkErsAqQSuwCpxC5AKrELkErsAqQSuwCpxC5AKrELkErsAqQSuwCpxC5AKrHLSb1qfr1S0IH9mJMqQ1bscg//ArFVP7YDzzmrAAAAAElFTkSuQmCC" alt="" />

  2.利用初始化器:

   

三、双问号与三目运算符。

   如果 list 为 null, 则 new 一个对象。

   1、一般做法是:

    AWT4zicgQ/wAGeIHyPBzW0CG+AEyxA+PEJ+4Ubskd7p7VStnmF5mu//47+fnVrq3v4l9W1XaOt3QXmdlLY6qP7mvPRuqZj17tu8Uz03/XlupId8CPc35XyY6V6ryxBG/e1slfmdrMFd/iviFIyfen+JZ7veI+NVP0r7fZZ7jvVS/c+bNuuRgfFd+SvzS/LHSaNAcziUjvcrYr7QiyWz9DSV1VuZMCpsDV/NZ9sxWKpzaI+JnFsb/lqYHLYxxsZfKpWDivyWVjiLffZvT39aLTXre+/kbKpU3IxpPxH8TnqHmekkzPTd+wbpESemxfQtjTJcu8BJGxy98ewxPDc7er7+h0sPNCvMOMJTj19P7refR8TPLPU+Mf/AZqh2dOefw+Jnl/e/9Ohs62vslhWb83jzOND03fncOPvNpszD4sheqo7vOMWFwH/l82uCTI5852ed+cafULDQrGbQ89vGV/K69MDi6vtKorF5o1lNY7PRzv4ENNQ+0mCubzLD//Uw0D5VV/m2Wz2u19cGk4pfd5jzLWG19gIkQP0CG+AEyxfjxc1vgasQPkKnF73N8n/gBF2n0fsQPuA7xA2SIHyBD/ACZ9pHPjev7Adeg9wNkiB8gQ/wAGeIHyPCtF0CG73wCMsQPkCF+gAw/twVkiB8gQ/wAGeIHyBA/QIb4ATLED5AhfoAM8QNkiB8gQ/wAGeIHyBA/QIb4ATLED5AhfoAM8QNk/gBFOvOv7g4QlAAAAABJRU5ErkJggg==" alt="" />

   2、利用双问号:

    WTPHeU+uP409Ap/pzHOn9Oz0e2+bPUQ98b/QV/llKUJHux5zMho611saT6cJXKu+l6N8ZC0hQrZaQaL1HdVcp7Y/27Sd4mwUYvTZnkZIMrRSaVzYWrD2SvfPRVbObZ+ZlPaLM8kZ2SnprzDvgwYVLIK+W1ROX8TJrkbal8j7513S1GUvuJ5u8Q1lRlDzjI0Dpd9yZneXacbZL6lvkqW1H+ZOtcf173gWnpwe4ltfLRJBv9mZ+WdX+OnJ+PAg4y6k+13vYpp1geWSqpb6DPGKWsPrfIn2r9+N+fg5M8aC3Vn+Pi7QEHIb+dSdOfebmpq5Kpdue3QkPPEXQ8v7Xn5937vhP0xVAzjXql2sk0lXsl9W7DaQabp7hJfLyWSP7BE53WrFT7Kci2zmfHJKuPq5EyOAmIV3lbIu+zGTbxQ52OX2UAfUiHN2PqYcfxqwxgD/gTwC/4EwB6wJ8AfsGfAH7BnwB++QGigDo3KXBeCgAAAABJRU5ErkJggg==" alt="" />

   3、利用三目运算符:

      

四、利用lamda表达式

  当需要计算俩个数的方法时

  1、一般做法是:

   

      2、利用lamda表达式:

   

  假如要求 3+5,可以直接调用 temp(3,5); 返回的结果是 8

 

   注: Func<int,int,int> 是系统定义好的有两个参数,一个返回值的泛型委托,下面是其定义:  

  

五、利用Linq。

  假如要筛选出 ItemCode 为 "hello" 的元素

  1、一般做法:

  JSym5U/ejZZF9NTwyk7l5KSY0Sonnw3WiqEbQTeKV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obtX5OqvdkWeZVieEtsVRjdUTPLT2Nnt3N3oI6W/UgE6KJ/bprvyO6BTrxpainWYCAKABAAqIAAAJEASpwifE2GG+ogCi8DcZ7aU5dkE7niMLb2HO8jVvrg1Pcv3vJ0ngQoDtWN54QiMLb2Hm8R2ZzSVDGRcFwbrecsTgRBcjZebyni4LfZ/wMn9NJVQJOWpxVt4jC29h5vD3Pz8bt8XO4hrE/tPGsrt2Y7GuklDSqlh1HlET3HDJsw87j3XpC9qyEblFQt42IntVr+EwSaApUjQt7s/N4e0QhNN7k84tCXhHk2+OioNYFAVGAAXYeb6coqO0jouBf/xMrhaQRUYBudh7vCy4fqlVGtyjYqVZ9OkWhVePgDew53qW62m5UnahunTatCXhUwE4pDp28dYbI2xGFt8F4L82sBan6MZwjCm+G8d6fUink331uPrA4jDdUQBTeBuMNAAJEAQAEiAIACNpEYeR+FQA8guYVjigA7A2iAAACRAEABIgCAAh6Vji6ALAxVAoAIEAUAECAKACAAFEAAAFPNAKAgBUOAAJEAQAEiAIACBAFABAgCgAgQBQAQIAoAIAAUQAAwV8ZeSRKnEr+EAAAAABJRU5ErkJggg==" alt="" />

  2、利用Linq

  

   注:1、Where是扩展方法 (通过this关键字定义),是系统定义好的函数,定义如下:

         a、第一个参数 source 对应上面的 list,

       b、 第二个参数 predicate ( 参数类型为Tsource,返回类型为 bool ),

         对应 lamda 表达式(o=>o.ItemCode == "hello")

  JVMhi3RDQDgFtIbAMBKZvKW6AYAcBfpDQBgJcORS3QDALiR9AYAsJKx1CW6AQDcS3oDAFiJ4AUAsBLpDQBgJdIbAMBKpDcAgJXckN7+/vfX+qk2bt0AAPhBp5LQ3FtQWw3i5ft/AQB+1nwYmv70kOTcW9CsNVEHAPD17klv0/2PdgIA8GUmM9CZj+0NmpV3mXUDANi7Ib1tI29cyCx5dZgcHQBgXTOJ55Lvyyr/yq3VLPnWVOkNAPgFT09v0/0DAHyl4Uj0vmumQ287HS0bAOA7fDq9tVbpdtu6/Voi0gEAv+DU5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