c#-6.0

Force Microsoft Build Tools 2015 to include mscorlib for the targeted version of the framework instead of 4.6

折月煮酒 提交于 2019-12-05 00:51:34
问题 I have written an application in Visual Studio 2015 that uses C# 6.0 features and targets .NET 4.5.2. When I build it using Microsoft Build Tools 2015, which is what is done by our TeamCity server, the resulting bin folder also contains a copy of mscorlib.dll . The problem here is that the mscorlib.dll being copied is the .NET 4.6 DLL, which causes problems at runtime. I have replaced my call to string.Format() with the new string interpolation syntax to work around the problem. That, however

call instead of callvirt in case of the new c# 6 “?” null check

久未见 提交于 2019-12-05 00:35:12
Given the two methods: static void M1(Person p) { if (p != null) { var p1 = p.Name; } } static void M2(Person p) { var p1 = p?.Name; } Why the M1 IL code use callvirt : IL_0007: brfalse.s IL_0012 IL_0009: nop IL_000a: ldarg.0 IL_000b: callvirt instance string ConsoleApplication4.Person::get_Name() and the M2 IL use call : brtrue.s IL_0007 IL_0004: ldnull IL_0005: br.s IL_000d IL_0007: ldarg.0 IL_0008: call instance string ConsoleApplication4.Person::get_Name() I just can guess that it because in M2 we know that p isn't null and its like new MyClass().MyMethod(); Is it true? If it is, what if p

Null propagation operator and dynamic variable

本小妞迷上赌 提交于 2019-12-04 16:24:03
问题 I have been looking at the null-propagation operator in C#6 and tried to make it work with the variables of dynamic type but without success. Consider the code below, it compiles but CLR throws AccessViolationException at runtime when the null-propagation is applied to dynamic object. class SomeType { public object SomeProperty { get; set; } static void Main() { var obj = new SomeType() { SomeProperty = "ABCD" }; var p1 = ((dynamic)obj).SomeProperty; //OK, p1 is set to "ABCD" var p2 = (

Using “nameof” operator in Razor views

人走茶凉 提交于 2019-12-04 15:25:54
问题 On my VS.NET 2015 development machine, the Razor views that use the nameof operator work like a charm. When deploying to a Windows server, it fails to compile the CSHTML Razor views: The name 'nameof' does not exist in the current context. In German: Der Name 'nameof' ist im aktuellen Kontext nicht vorhanden. I've installed .NET Framework 4.6 ("ASP.NET version: 4.6.81.0") with no success. Most likely I'm missing something plain simple. Still, I'm really clueless. Is it a web.config setting?

String interpolation issues

ε祈祈猫儿з 提交于 2019-12-04 14:59:56
问题 I'm trying to figure out why my unit test fails (The third assert below): var date = new DateTime(2017, 1, 1, 1, 0, 0); var formatted = "{countdown|" + date.ToString("o") + "}"; //Works Assert.AreEqual(date.ToString("o"), $"{date:o}"); //Works Assert.AreEqual(formatted, $"{{countdown|{date.ToString("o")}}}"); //This one fails Assert.AreEqual(formatted, $"{{countdown|{date:o}}}"); AFAIK, this should work correctly, but it appears that it doesn't pass the formatting parameter in correctly, it

Trying to understand ?. (null-conditional) operator in C#

倖福魔咒の 提交于 2019-12-04 14:55:48
问题 I have this very simple example: class Program { class A { public bool B; } static void Main() { System.Collections.ArrayList list = null; if (list?.Count > 0) { System.Console.WriteLine("Contains elements"); } A a = null; if (a?.B) { System.Console.WriteLine("Is initialized"); } } } The line if (list?.Count > 0) compiles perfectly which means that if list is null , the expression Count > 0 becomes false by default. However, the line if (a?.B) throws a compiler error saying I can't implicitly

Dynamically load assemblies in ASP.NET 5

ⅰ亾dé卋堺 提交于 2019-12-04 12:43:22
问题 I used to have some code which scanned the bin directory of my application for assemblies which weren't loaded in the AppDomain yet and loaded them. It basically looked like: foreach (var assemblyPath in Directory.GetFiles("path\to\bin", "*.dll")) { var inspected = Assembly.ReflectionOnlyLoadFrom(assemblyPath); Assembly.Load(inspected.GetName()); } I skipped the try/catch clauses, etc for brevity. This allowed me to drop assemblies in the bin folder at run-time with implementations for

RyuJit producing incorrect results

戏子无情 提交于 2019-12-04 09:07:25
问题 After recently upgrading to .net 4.6 we discovered a bug where RyuJit produces incorrect results, we were able to work around the issue for now by adding useLegacyJit enabled="true" to the app.config. How can I debug the machine code generated by the following? I created a new console project in VS 2015 RTM, set to Release, Any CPU, unchecked Prefer 32 bit, running with and without debugger attached produces the same result. using System; using System.Runtime.CompilerServices; namespace

Is there a way to Imitate C# 6 Null-Conditional operator in C# 5

假装没事ソ 提交于 2019-12-04 08:57:26
I have a situation where I need to assign some objects' properties inside an object initializer. Some of these objects can be null and I need to access their properties, the problem is that they are too many, and using a if/else thing is not good. Example visits = visitJoins.AsEnumerable().Select(joined => new VisitPDV() { VisiteId = joined.Visite.VisiteId.ToString(), NomPointDeVente = joined.VisitePdvProduit.PointDeVente.NomPointDeVente, }); The joined.VisitePdvProduit can be null, and the problem is that there are like dozens of such assignments (I just took one to shorten the code) The C# 6

Error when using the null-propagating / null-conditional operator

血红的双手。 提交于 2019-12-04 03:28:13
问题 I am running a .NET 4.5 project in VS 2013. Why is the following code in error? var w = Request.Properties["MS_HttpContext"] as System.Web.HttpContextWrapper; string IP = w?.Request.UserHostAddress; //fail to compile I found this code on this MSDN blog. 回答1: That is a new feature available in C# 6 and newer versions. It is called the null-conditional operator. In order to use C# 6 you should download Visual Studio 2015 or a newer version, since the extension for Visual Studio 2013 isn't