今天我有一个问题想问。C#6.0或其他任何版本都引入了很酷的新功能,使编码更容易。但是假设我的客户运行的是不支持.NET 4.6的WindowsXP系统。如果我使用C#6.0(或以后版本)编写代码,但将其编译为.NET 2.0框架(或其他与发布版本不同的.NET版本),是否存在缺点?
编辑:对于那些不知道如何进行操作的人,您可以通过项目属性->应用程序选项卡->目标框架来指定.NET Framework。而C#语言版本则可通过项目属性->构建选项卡->高级选项->语言版本来指定。
编辑2:在.NET 2.0上编译的C#6.0代码可以运行在WindowsXP上。
编辑:对于那些不知道如何进行操作的人,您可以通过项目属性->应用程序选项卡->目标框架来指定.NET Framework。而C#语言版本则可通过项目属性->构建选项卡->高级选项->语言版本来指定。
编辑2:在.NET 2.0上编译的C#6.0代码可以运行在WindowsXP上。
static void Main(string[] args)
{
try
{
StringBuilder sb = null;
string value = sb?.ToString(); // C#6 Feature. Wont throw an exception.
if(value == null)
{
Console.WriteLine("The Value is null");
}
string value2 = sb.ToString(); // Will cause an "Object Reference not set to an instance" exception
}
catch ( Exception ex) when (ex.Message.Contains("object")) // C#6.0 conditional catches
{
Console.WriteLine("Exception Caught");
}
catch(Exception ex)
{
Console.WriteLine("Other Exception");
}
Console.ReadLine();
}