C#(011):C# 5.0 新特性(.NET Framework 4.5 与 Visual Studio 2012 )(C # (011): C # 5.0 new features (. Net framework 4.5 and visual studio 2012))

一、C#新增的小功能

1、绑定运算符:=:

这个只是简化了数据绑定,跟ASP.NET MVC3不断改进一样,其实不是什么亮点改进。

comboBox1.Text :=: textBox1.Text; //将文本框的内容绑定到下拉框。  

2、带参数的泛型构造函数

这个的加入给一些设计增加了强大功能,泛型早在C#2.0加入后就有着强大的应用,一般稍微设计比较好的框架,都会用到泛型,C#5.0加入带参数泛型构造函数,则在原有基础上对C#泛型完善了很多。

public class T MyClass : T: class, new()  
public class T MyClass : T:class, new(int)  

3、case支持表达式

这个是一个我很早就想如果能这样就好了,没想到在C#5.0里就加入此功能,以前case里只能写一个具体的常量,而现在可以加表达式了,灵活多了。

switch(myobj){
  case string.IsNullorEmpty(myotherobj):
 ..... 
  case myotherobj.Trim().Lower: 
 ....
}

4、扩展属性

我们在C#3.0里有扩展方法,那么在C#5.0里将会加入扩展属性的感念,对照扩展方法,不难理解扩展属性的概念了。以下为扩展属性的定义举例:

[Associate(string)]
public static int MyExtensionProperty { get;set;}

5、支持null类型运算

int x? = null;
int y? = x + 40;

Myobject obj = null;
Myotherobj obj2 = obj.MyProperty ??? new Myotherobj();

二、Asynchronous methods 异步方法

见专题。

三、Caller info attributes:调用时访问调用者的信息

为了便于调试,C#5.0提供了一种新特性:CallerInfoAttributes。

它包括三个主要的类:

  • [CallerMemberName] :返回调用函数的名称。
  • [CallerFilePath] :返回调用函数所在源文件全路径信息 。
  • [CallerLineNumber] :返回调用函数调用具体行号。

该功能主要是用于调试,示例代码如下:

在本示例中,我们添加了一个Log函数,以对程序进行日志记录,并在主函数中进行调用。

using System;
using System.Runtime.CompilerServices;

namespace TestPro
{
    class Program
    {
        public static void Main()
        {
            Log("Test.");
        } 

        // 对日志消息进行记录,同时所有内容均有默认值,如果获取失败,则使用默认值。
        public static void Log(string message,
            [CallerMemberName] string callerName = "unknown", 
            [CallerFilePath] string callerFilePath = "unknown", 
            [CallerLineNumber] int callerLineNumber = -1)
        {
            Console.WriteLine("Message: {0}", message);
            Console.WriteLine("Caller's Name: {0}", callerName);
            Console.WriteLine("Caller's FilePath: {0}", callerFilePath);
            Console.WriteLine("Caller's LineNumber: {0}", callerLineNumber);
        }
    }
}

程序执行以后会显示以下内容。

Message: Test.
Caller Name: Main
Caller FilePath: C:\Users\Administrator\source\repos\TestPro\Program.cs
Caller Line number: 10
请按任意键继续. . .
————————

1、 C # new small functions

1. Binding operator: =:

This only simplifies data binding, which is similar to ASP Net mvc3 continues to improve. In fact, it is not a bright spot.

comboBox1.Text :=: textBox1.Text; //将文本框的内容绑定到下拉框。  

2. Generic constructor with parameters

The addition of this adds powerful functions to some designs. Generics have powerful applications as early as the addition of c#2.0. Generics are generally used in slightly better designed frameworks. Adding a generic constructor with parameters in c#5.0 improves c# generics a lot on the original basis.

public class T MyClass : T: class, new()  
public class T MyClass : T:class, new(int)  

3. Case supports expressions

This is a function I thought it would be better if I could do this for a long time. I didn’t expect to add this function in c#5.0. In the past, only one specific constant could be written in case, but now I can add an expression, which is much more flexible.

switch(myobj){
  case string.IsNullorEmpty(myotherobj):
 ..... 
  case myotherobj.Trim().Lower: 
 ....
}

4. Extended properties

We have an extension method in c#3.0, so we will add the idea of extension attribute in c#5.0. Compared with the extension method, it is not difficult to understand the concept of extension attribute. The following is an example of the definition of extended attributes:

[Associate(string)]
public static int MyExtensionProperty { get;set;}

5. Support null type operation

int x? = null;
int y? = x + 40;

Myobject obj = null;
Myotherobj obj2 = obj.MyProperty ??? new Myotherobj();

二、Asynchronous methods 异步方法

See topic.

三、Caller info attributes:调用时访问调用者的信息

To facilitate debugging, c#5.0 provides a new feature: callerinfo attributes.

It includes three main classes:

  • [CallerMemberName] :返回调用函数的名称。
  • [callerfilepath]: returns the full path information of the source file where the calling function is located.
  • [callerlinenumber]: returns the specific line number of the calling function call.

This function is mainly used for debugging. The example code is as follows:

In this example, we added a log function to log the program and call it in the main function.

using System;
using System.Runtime.CompilerServices;

namespace TestPro
{
    class Program
    {
        public static void Main()
        {
            Log("Test.");
        } 

        // 对日志消息进行记录,同时所有内容均有默认值,如果获取失败,则使用默认值。
        public static void Log(string message,
            [CallerMemberName] string callerName = "unknown", 
            [CallerFilePath] string callerFilePath = "unknown", 
            [CallerLineNumber] int callerLineNumber = -1)
        {
            Console.WriteLine("Message: {0}", message);
            Console.WriteLine("Caller's Name: {0}", callerName);
            Console.WriteLine("Caller's FilePath: {0}", callerFilePath);
            Console.WriteLine("Caller's LineNumber: {0}", callerLineNumber);
        }
    }
}

After the program is executed, the following contents will be displayed.

Message: Test.
Caller Name: Main
Caller FilePath: C:\Users\Administrator\source\repos\TestPro\Program.cs
Caller Line number: 10
请按任意键继续. . .