c#-6.0

How to create a static lambda for use with expression building?

瘦欲@ 提交于 2019-12-05 10:43:06
As of C# 6, lambdas now default to instance methods, and will never be static (which I assume means they always capture now, which I guess is more efficient [seems to be faster given the discussions]). See here: Why has a lambda with no capture changed from a static in C# 5 to an instance method in C# 6? and here: Difference in CSC and Roslyn compiler's static lambda expression evaluation? This causes issues now with using lambdas when creating static MethodInfos for calls to expression methods such as Expression.Convert(Expression, typeof({SomeType}), conversionMethodInfo); So then, what is

Specify decimal places using variables inside string interpolation

血红的双手。 提交于 2019-12-05 08:54:45
I have a string format which includes two integer variables, each of which needs to be formatted to a variable length: int x = 1234; int y = 42; // Simplified, real values come from method outputs, so must use the variables: int xFormatDigitCount = 7; int yFormatDigitCount = 3; var xStringFormat = new string('0', xFormatDigitCount); // "0000000" var yStringFormat = new string('0' ,yFormatDigitCount); // "000" For now I only managed to get the desired format using the integer variables' .ToString() methods: var xString = x.ToString(xStringFormat); var yString = y.ToString(yStringFormat); return

The new null-conditional operator in ASP.NET MVC Razor

巧了我就是萌 提交于 2019-12-05 08:22:23
问题 So since C# 6.0 came out, I've been using the null-conditional operator quite a lot. Example: Model?.Person?.Zip However, I now have a situation where I have a solution where the customer operates on domain models in the view. While I would hunt down the developer with an axe, I find it easier to just do some null checks in the view. However, when I go this in Razor: @Model?.Person?.Zip My Model? is seen as dynamic, but ? breaks the dynamic things and rest is rendered as text. How do you

Enabling C# 6 in ASP.VNext projects in Visual Studio CTP2

旧街凉风 提交于 2019-12-05 07:15:42
I have installed Visual Studio CTP2 and created a new ASP.Net Vext project. When I tried using C# 6.0 features, it was not working. I even tried the stpes in the following link. No C# 6.0 in Visual Studio 2015 CTP? But even after this I was not able to use C# 6 in VNext projects. Please help. Add this to your project.json: "compilationOptions": { "languageVersion": "experimental" } You should not add the net451 object. Use this inside the project.json file: { "compilationOptions": { "languageVersion": "experimental"}, "dependencies": { }, "commands": { } } I believe they also changed the

New C# 6 object initializer syntax?

依然范特西╮ 提交于 2019-12-05 06:53:26
I just noticed that the following is possible in C# written in Visual Studio 2015, but I've never seen it before: public class X { public int A { get; set; } public Y B { get; set; } } public class Y { public int C {get; set; } } public void Foo() { var x = new X { A = 1, B = { C = 3 } }; } My expectation was for Foo to have to be implemented like this: public void Foo() { var x = new X { A = 1, B = new Y { C = 3 } }; } Note that there is no need to call new Y . Is this new in C# 6? I haven't seen any mention of this in the release notes , so maybe it's always been there? You will get a

Null propagation operator

南笙酒味 提交于 2019-12-05 06:32:35
I've looked around a bit but haven't been able to find an answer to how the new C# 6.0 compiler breaks down the new null propagation command for something such as the following: BaseType myObj = new DerivedType(); string myString = (myObj as DerivedType)?.DerivedSpecificProperty; What I want to know is how exactly it handles this. Does it cache the as cast into a new DerivedType variable (i.e., this is just syntactical sugar for an as cast followed by an null comparison). Or if it actually as cast it, check for null, then if not null, recast and keep going. Does it cache the as cast into a new

Why does the IL set this value twice?

淺唱寂寞╮ 提交于 2019-12-05 05:38:13
I was trying around a bit with Try Roslyn when I entered this piece of code: using System; using System.Linq; using System.Collections.Generic; using Microsoft.CSharp; public class C { public C() { x = 4; } public int x { get; } = 5; } And it gave me back this code: using System; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Security; using System.Security.Permissions; [assembly: AssemblyVersion("0.0.0.0")] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations |

Auto-property initializer Singleton implementation

偶尔善良 提交于 2019-12-05 04:07:30
So, with the brand new C# 6 we got those neat auto-property initializers. I thought I might as well take advantage of these to make more concise singletons than ever. Apparently someone else got that idea, too. public sealed class Singleton { public static Singleton Instance { get; } = new Singleton(); private Singleton() { /* some initialization code */ } } My questions are: How thread-safe it is? How lazy it is, or when the instance is actually created? (not a priority, but it would be good for future reference) Is it a good idea overall? (it might look similar to this question , but it's

How does nameof work?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 03:51:26
I was just wondering how come nameof from C# 6, can access non static property just like if it was static. Here is an example public class TestClass { public string Name { get; set; } } public class Test { public Test() { string name = nameof(TestClass.Name); // whats so speciall about nameof //string name2 = TestClass.Name; this won't compile obviously, } } It's not "accessing" the property - that operator is purely a compiler mechanism to inject the "name" of the argument into the code. In this case it will replace nameof(TestClass.Name) with "Name" . The fact that it's non-static is

Why is declaration expression dropped in C# 6?

落花浮王杯 提交于 2019-12-05 03:34:18
In the preview for C# 6, Microsoft introduced the syntactic sugar for declaring out parameter inline as in seen in this article http://odetocode.com/blogs/scott/archive/2014/09/15/c-6-0-features-part-3-declaration-expressions.aspx Does anybody know why this feature is dropped in the release version of .NET 4.6? The explaination is in this codeplex topic. Hi all, As we enter the final stage in our long quest to renew the C# and Visual Basic experience, we’ve had to make some tough decisions around the set of language features that will make it into the next version of the languages. These