c#-6.0

C# 6.0 Features Not Working with Visual Studio 2015

心已入冬 提交于 2019-12-17 02:34:22
问题 I am testing Visual Studio 2015 with C# 6.0 but the language features are not working. In an MVC web application, the following code does compile: if (!string.IsNullOrWhiteSpace(Model.Profile?.TypeName)) { // More logic here... } However, when I run the application via Debug and IIS Express, I get the following error: CS1525: Invalid expression term '.' How do I enable these features? 回答1: This works in MVC 5 (tested 5.2.3), you just need to add the roslyn code dom Nuget package CodeDOM

C# 6 error messages on VS2013 despite using Microsoft.Net.Compilers nuget package

安稳与你 提交于 2019-12-13 13:18:42
问题 Over half of our 50 man development team still uses Visual Studio 2013. Despite this, we would like to use C# 6. So we tried using this solution: https://stackoverflow.com/a/32010632/3997704. Compilation with Microsoft.Net.Compilers works fine, as does our CI environment. However, Visual Studio shows a lot of errors in the Error List related to C# 6 features. I tried getting rid of the errors by using clean solution, rebuild, restarting Visual Studio and clearing ReSharper caches but none

Deserialize json to list of KeyValue pairs [duplicate]

孤人 提交于 2019-12-13 04:11:12
问题 This question already has answers here : How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET? (21 answers) Closed last year . I have the following json: [ { "key":"key1", "value":"val1" }, { "key":"key2", "value":"val2" } ] How can I deserialize it into an list/array of NameValuePair<string, string> ? Example: var json = "[{\"key\":\"key1\",\"value\":\"val1\"},{\"key\":\"key2\",\"value\":\"val2\"}]"; var serializer = new JavaScriptSerializer(); var result = serializer

Automatically implemented properties must define both get and set accessors

 ̄綄美尐妖づ 提交于 2019-12-12 20:23:44
问题 SQLCLR Visual Studio 2015 I'm new to writing CLR code. I'm getting the following error when compiling a SQL CLR Function I'm using the .Net Coordinates library. The code in question is public Datum.Datum Datum { get; } The same library when compiled in a C# console application (not CLR) builds and executes successfully using Visual Studio 2015. My understanding is that by using Visual Studio I'm using C# v6. Could the .sqlproj be forcing the use of a earlier version of C#? 回答1: The main issue

Public readonly field v.s. get-only property

匆匆过客 提交于 2019-12-12 07:17:00
问题 Are there cases when you would want a public readonly field v.s. a get-only auto-implemented property? public class Foo { public readonly string Hello; public string Hello2 { get; } } Both can only be set during the constructor and both offer readonly access outside of the class.. I'm a little tired so I might be missing something. 回答1: One reason would be for data binding - .net implements binding to properties but not to public fields. Some discussion here : Why can't we use public fields

Setting C# 6.0 rules in SonarQube 5.1

廉价感情. 提交于 2019-12-12 03:32:33
问题 I installed SonarQube version 5.1 and running it against C# code. The results of the analysis were quite OK, but i was expecting that Sonar would point out the issues based on the latest C# 6.0( which is current now) features too, which seems missing. Can anyone let me know if i can add new C#6.0 features in some sort of plugin? I am not sure if i can add custom rules for C#, as i couldn't find in their website for c#( found here- http://docs.sonarqube.org/display/DEV/Writing+Custom+Rules

Long string interpolation lines in C#6 don't support Tab,CR and LF

∥☆過路亽.° 提交于 2019-12-11 07:22:53
问题 I tried to use string interpolation $"" in C#6 with tabs var name="My Name"; var text =$"\t\t{name}"; and it's working fine and tabs \t is resolved. When trying to use Long string interpolation lines var name = "myname"; var text = $@"\t\t{name} tab and name is in a Long string interpolation \r\n "; Console.WriteLine(text); output \t\tmyname tab and name is in a Long string interpolation \r\n The tab \t ,\r and \n aren't resolved So i had to use string.Format() to resolve this issue. The

Serialize get-only properties on MongoDb

送分小仙女□ 提交于 2019-12-11 05:42:23
问题 With C# 6 I can write: public class Person { public Guid Id { get; } public string Name { get; } public Person(Guid id, string name) { Id = id; Name = name; } } Unfortunately a class like this is not serialized correctly by MongoDb driver, properties are not serialized. MongoDb only serialize by default properties with getter and setter. I known that you can manually change the class mapping and tell serializer to include get-only properties but I was looking for a generic way to avoid

C# quotes in interpolated string Unexpected character \0022

大憨熊 提交于 2019-12-10 20:27:38
问题 I've read that you can use expressions in interpolated strings, but escaping quotes does not work. private string sth = $"{String.Join(\"\", Node.stringToType.Keys)}"; Error CS1056: Unexpected character `\0022' (CS1056) Error CS1525: Unexpected symbol `)', expecting `${', `:', or `}' (CS1525) UPDATE: The inner expression above was ment to be equivalent to String.Join("", Node.stringToType.Keys) (the two backslashes were for escaping the two double quotes) like that you can insert there any

Is it possible to overload the nameof operator in c#?

陌路散爱 提交于 2019-12-10 19:40:43
问题 Can I overload the nameof operator in C#? The C# programming guide is outdated and the C# 6 under the hood doesn't help me. How can I overload the nameof operator? 回答1: The nameof operator is evaluated at compile-time. The nameof expression is a constant. In all cases, nameof(...) is evaluated at compile-time to produce a string. Its argument is not evaluated at runtime, and is considered unreachable code (however it does not emit an "unreachable code" warning). So, these operator cannot be