在使用Linq扩展时,看到这样的代码是正常的:
IEnumerable<int> enumerable = GetEnumerable();
int sum = 0;
if (enumerable != null)
{
sum = enumerable.Sum();
}
Run Code Online (Sandbox Code Playgroud)
为了提高代码质量,我编写了以下扩展方法来检查可以为空的枚举并打破linq执行.
public static IEnumerable<T> IgnoreIfEmpty<T>(this IEnumerable<T> enumerable)
{
if (enumerable == null) yield break;
foreach (var item in enumerable)
{
yield return item;
}
}
Run Code Online (Sandbox Code Playgroud)
所以,我可以重构代码,如下所示:
var sum = GetEnumerable().IgnoreIfEmpty().Sum();
Run Code Online (Sandbox Code Playgroud)
我现在的问题:
更新: 我的目标框架是:3.5
是否可以在旧版.NET Runtime中使用Roslyn编译器和C#6.0的新功能(例如,.NET 4.0)?
例如,我想在.NET 4.0应用程序中使用表达式成员(int S => x + y;而不是int S { get { return x + y; } }).
重写以下代码是否安全:
bool b = foo.bar.HasValue ? foo.bar.Value : false;
Run Code Online (Sandbox Code Playgroud)
至
bool b = foo.bar.Value ?? false;
Run Code Online (Sandbox Code Playgroud)
哪里bar是可空的类型bool?
当我尝试使用.NET 2.0或3.0运行时添加扩展方法时,我收到错误:
无法定义新的扩展方法,因为无法找到编译器所需的类型"System.Runtime.CompilerServices.ExtensionAttribute".您是否缺少对System.Core.dll的引用?
但是当我尝试将它添加到项目中时,我无法在可用引用列表中找到System.Core.我需要做什么才能在我的项目中使用扩展方法和LINQ on?
我有一个针对旧版.NET Framework(.NET 4.5.2)的项目.我安装了Visual Studio 2015(因此我的机器上安装了.NET 4.6).我注意到如果我使用.NET 4.6/C#6中发布的C#语言功能,它仍会编译.如果我的项目的目标框架是<.NET 4.6,那么这不应该编译:
public string MyExpressionBodyProperty => "1";
//auto properties are new in C# 6
public string MyAutoProperty { get; } = "1";
private static void MethodThatUsesNameOf(string filename)
{
if (filename == null)
{
//nameof was released in C# 6
throw new ArgumentException("The file does not exist.", nameof(filename));
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能确保我只使用与我所针对的框架版本一起使用的.NET语言功能?
阅读以下内容后:
在我看来,除了String Interpolation,我在VS2015中针对.NET 4.51编译的任何项目都可以使用新的C#语言功能.
但是我使用VS2015定位4.51在我的开发机器上尝试了以下代码:
string varOne = "aaa";
string varTwo = $"{varOne}";
if (varTwo == "aaa")
{
}
Run Code Online (Sandbox Code Playgroud)
并且我不仅没有收到编译器错误,它的工作原理是varTwo 包含aaa,如预期的那样.
有人可以解释为什么会这样,因为我不希望这个工作吗?我猜我错过了FormattableString的真正含义.有人能举个例子吗?
我在Visual Studio 2015中遇到了一个奇怪的行为.我正在编写一个针对4.5.2 .Net Framework的应用程序:
然后在应用程序中,我尝试使用在C#6.0(来自.Net 6.0)中实现的一些东西:
int number = 456;
var name = nameof(number);
string str = $"Time = {DateTime.Now}";
Run Code Online (Sandbox Code Playgroud)
我很确定nameof使用的字符串格式$仅在.Net 4.6中可用,但我仍然可以编译此代码 - 尽管我的目标是4.5.2.
我在Visual Studio 2013中运行相同的代码,也针对4.5.2版本,但这次我无法编译代码(nameof未找到).
怎么可能,VS2015允许我编译目标.Net Framework版本中不应该允许的代码?
我在黑暗中拍摄的是VS2015能够将C#6.0代码计算到CIL中,这仍然与以前的版本兼容,但我不确定它是否是正确的猜测.
据推测,Null条件运算符已在C#6.0(或.NET Framework 4.6)中引入.但是,我的项目使用框架的4.5.2版本(C#5.0).当我使用Visual Studio 2015时,IDE似乎理解了新的符号并且编译/正常工作.但是,我对该项目是否仍然适用于发布感到困惑.我的开发机器安装了4.6但生产服务器可能没有.
换句话说,Null条件运算符是否真的与4.5.2兼容或者我错过了什么?
这是我的代码,我想要前后单引号.
char[] charsToTrim = { '\\' };
selectedITeration += (lis.Text.Trim(charsToTrim) + ",").Replace(@"\", @"\\");
Run Code Online (Sandbox Code Playgroud)
输出:
\MRI_SCRUM_GIT\\Iteration\\AS-Automation,
\MRI_SCRUM_GIT\\Iteration\\Pluse Pheonix,
\MRI_SCRUM_GIT\\Iteration\\Pluse Pheonix\Sprint 1,
\MRI_SCRUM_GIT\\Iteration\\Pluse Pheonix\Sprint 10
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西:
'MRI_SCRUM_GIT\\AS-Automation',
'MRI_SCRUM_GIT\\Pluse Pheonix',
'MRI_SCRUM_GIT\\Pluse Pheonix\\Sprint 1',
'MRI_SCRUM_GIT\\Pluse Pheonix\\Sprint 10'
Run Code Online (Sandbox Code Playgroud) 我需要使用Jenkins进行Continues集成,在我的代码中我编写了类似的代码
throw new ArgumentNullException($"The {nameof(Id)} cannot be null");
Run Code Online (Sandbox Code Playgroud)
如果我构建应用程序它正在构建成功,但如果我使用MsBuild(或)Jenkins构建相同的应用程序,
我得到的错误就像
Web\WebSharedHelper.cs "C:\WINDOWS\TEMP\.NETFramework,Version=v4.5.2.AssemblyAttributes.cs"
Security\ApiUserToken.cs(46,32): error CS1056: Unexpected character '$' [C:\Program Files (x86)\Jenkins\workspace\OssiaCICD\common\trunk\Cota.Common.Core\Cota.Common.Core.csproj]
Security\ApiUserToken.cs(58,32): error CS1056: Unexpected character '$' [C:\Program Files (x86)\Jenkins\workspace\OssiaCICD\common\trunk\Cota.Common.Core\Cota.Common.Core.csproj]
Security\ApiUserToken.cs(62,26): error CS1056: Unexpected character '$' [C:\Program Files (x86)\Jenkins\workspace\OssiaCICD\common\trunk\Cota.Common.Core\Cota.Common.Core.csproj]
Done Building Project "C:\Program Files (x86)\Jenkins\workspace\OssiaCICD\common\trunk\Cota.Common.Core\Cota.Common.Core.csproj" (default targets) -- FAILED.
Run Code Online (Sandbox Code Playgroud)
我正在使用v4.0.30319 FrameWork for MSBuild.