现在的位置: 首页 > 综合 > 正文

Net 3.0学习

2014年07月21日 ⁄ 综合 ⁄ 共 1557字 ⁄ 字号 评论关闭

Net 3.0

<1>隐式声明变量:
var num=0;
var nums=new []{1,2,3,4,5};
var str="";
var list=new List<int>();
foreach(var i in nums)
{
    num+=i;
}

<2>扩展方法:
namespace NExplus.CSharp3Feature
{
  public static class MethodExtend
  {
     public static bool OutLength(this string str)
     {
         if(str.Length>10)
         {
   return true;
         }
         return false;
     }
  }
}

 注:
 扩展的方法必须在静态类里来扩展
 this
 string
 
<3>Lambda表达式:

1.1
public MyForm()
{
 listBox = new ListBox();
 textBox = new TextBox();
 addButton = new Button();
 addButton.Click += new EventHandler(AddClick);
}
void AddClick(object sender, EventArgs e)
{
 listBox.Items.Add(textBox.Text);
}

2.0
public MyForm()
{
 listBox = new ListBox();
 textBox = new TextBox();
 addButton = new Button();
 addButton.Click += delegate
 {
  listBox.Items.Add(textBox.Text);
};

3.0
public MyForm()
{
   addButton.Click=>listBox.Items.Add(textBox.Text);
}

<4>LinQ:

LinQ是.NET3.0发布的内置的一个扩展方法,
其实整个命名空间都是一个扩展方法的库,
它允许开发人员对任何数据类型进行查询.
大家都认为这是数据查询的趋势.
这些扩展的方法放在System.Linq命名空间下,
当我们创建一个新类型时,你会发现默认的命名空间中多了一个System.Linq.
LinQ定义了标准的查询操作符扩展方法.
可以为.NET开发人员用来轻松地查询XML,关系数据库,.NET 对象,和任何其他数据结构类型.

例:LinQ分别对不同类型的数据进行查询

由于VSMar07CTP有一个BUG,我们再看下面的例子时首先要去掉这个BUG。
在我查看M$Connect时发现已经有人提交了这个BUG,就是web.config中编译通不过,
其实去掉那个很简单,我们打开web.config找到<compiler>节点,把子节点注释掉,
OK去掉了BUG后我们就可以正常的编译调试了,如下:

1     <system.codedom>
2         <compilers>
3             <compiler language="c#;cs;csharp" extension=".cs"
 type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0,
 Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
4        
5                 <!--<providerOption name="CompilerVersion" value="v3.5"/>
6             </compiler>-->
7         </compilers>
8     </system.codedom>
9  

抱歉!评论已关闭.