c#-6.0

What do dollar symbols in C# Code mean?

大憨熊 提交于 2019-12-29 08:52:28
问题 Today, I pull the code from my client, and I get an error in this line. throw new Exception($"One or more errors occurred during removal of the company:{Environment.NewLine}{Environment.NewLine}{exc.Message}"); This line also moreCompanies = $"{moreCompanies},{databaseName}"; The $ symbols is so weird with me. This is C# code. 回答1: The $ part tells the compiler that you want an interpolated string. Interpolated strings are one of the new features of C# 6.0. They allow you to substitute

Why is this enum declaration working now?

我们两清 提交于 2019-12-29 08:51:40
问题 While answering another question, Jon Skeet mentioned that there is a weird thing going on with the definition of enums . His answer. He states that redifining the underlying type of an enum is only possible with the type-aliases and not with the framework types ( int is valid, Int32 not, etc.) public enum Foo : UInt32 {} // Invalid public enum Bar : uint {} // Valid Now I tried to reproduce that (with C#6/Roslyn in VS2015), and I didn't come to the same conclusion: public enum TestEnum :

Can I use null conditional operator instead of classic event raising pattern?

房东的猫 提交于 2019-12-29 01:35:51
问题 C# 6.0 adds this new ?. operator which now allows to invoke events like so: someEvent?.Invoke(sender, args); Now, from what I read, this operator guarantees that someEvent is evaluated once. Is it correct to use this kind of invocation instead of the classic pattern: var copy = someEvent if(copy != null) copy(sender, args) I'm aware of certain scenarios where above version of pattern would require additional locks, but let's assume the simplest case. 回答1: Yes See Null-conditional Operators on

Exclude auto properties from Code Coverage in Visual Studio 2015

两盒软妹~` 提交于 2019-12-28 15:50:10
问题 I just upgraded a bunch of projects to VS2015/C#6. Now MSTest's Code Coverage analysis is reporting that some auto properties aren't covered by unit tests. This wasn't the case in Visual Studio 2013, and I suspect it may be something to do with the new autoproperty features in C#6. Dealing with all the false-positives this generates rather defeats the purpose of the Code Coverage tool as it makes it practically impossible to identify actual code lacking test coverage. We don't want to write

How do I make MonoDevelop recognize nameof syntax from C# 6.0?

那年仲夏 提交于 2019-12-25 02:24:28
问题 I'm in MonoDevelop v5.9.6. Although it seems to support C# 6.0, the editor doesn't recognize the nameof keyword, and it marks it red, because it tries to recognize it as if it were an identifier. Is there any hack I can use to make it work in the editor, without breaking the compilation somehow? 回答1: This hack works: // hack to make MonoDevelop recognize nameof syntax from C#6.0 using nameof = System.Func<string>; The editor recognizes it as "returning a string" and doesn't give any errors

getting duplicate rows for each row using linq query

南笙酒味 提交于 2019-12-25 00:46:20
问题 I have db schema having tables like this below, i am trying to get the results using left join or normal join on these table using the below query. Somehow getting duplicate rows like as mentioned below. Requests (table) RequestStage (table) ------------- ---------------- id RequestStageid createdAt(datetime) name (values: RequestSubmitted,Inreview) RequestTypeId MasterSectionID RequestStatusID RequestStageID id (FK)(localCodes) MasterSections (table) ------------------------- MasterSectionId

Why doesn't the compiler give any errors or warnings when using this hack?

烂漫一生 提交于 2019-12-24 18:29:21
问题 In my other question, I found a hack to make this syntax work in MonoDevelop editor: // hack to make MonoDevelop recognize nameof syntax from C#6.0 using nameof = System.Func<string>; The C# compilers (Mono and VS) don't give any warnings or errors, and usages of the nameof keyword also work normally. My question is why. 回答1: I'm not a language lawyer but I believe the reason your code works is that nameof is a contextual keyword Let's take a step back to a more general case. If you try to

.NET Core IHttpContextAccessor issue

柔情痞子 提交于 2019-12-24 02:16:52
问题 I have static helper class public static class Current { public static string Host { get { return "httpContextAccessor here"; } } } How I can get access to current HttpContext inside Host property? 回答1: You can't and you shouldn't. This beats the whole purpose of having a dependency injection system at all. Static classes (for runtime data or Service Locator) is an anti-pattern. In ASP.NET Core you have to inject IHttpContextAccessor in classes where you need it. You can make a non-static

How do I enable C# scripting in VS 2015?

做~自己de王妃 提交于 2019-12-23 08:50:03
问题 How do I configure VS 2015 to enable Roslyn's C# scripting capabilities? I've tried installing various Nuget packages, including both the 1.0 and 1.1.0-beta1 versions of Microsoft.CodeAnalysis.CSharp, Microsoft.CodeAnalysis.Scripting.CSharp, etc., but I can't get any of the examples I've found online to work. I am getting "type not found" errors, i.e., var scriptEngine = new ScriptEngine(); ... fails because the type "ScriptEngine" is not found. Can someone provide as recipe that includes

What's the difference between ReSharper `MergeSequentialChecks` and `MergeSequentialChecksWhenPossible`?

梦想与她 提交于 2019-12-23 08:30:12
问题 I'm trying to figure out what's the difference between these two rules? MergeSequentialChecks MergeSequentialChecksWhenPossible The documentation doesn't say anything about the second one. https://www.jetbrains.com/help/resharper/2016.1/MergeSequentialChecks.html And it's not quire clear for me what does it mean WhenPossible ? If ReSharper suggests to apply the first rule and merge my sequential checks, then it IS possible indeed. How it could be not possible? Here is a code example to check.