c#-6.0

What benefit does the new “Exception filter” feature provide?

瘦欲@ 提交于 2019-12-17 18:25:29
问题 C# 6 has a new feature called "exception filtering" The syntax is like this: catch (Win32Exception exception) when (exception.NativeErrorCode == 0x00042) { //Do something here } I couldn't help but wonder what the benefit of that is over the current approach: catch (Win32Exception exception) { if (exception.NativeErrorCode == 0x00042) { //Do something here } } Is it a big deal that filtering happen before the curly bracket? Perhaps in relation to performance or security? 回答1: The Exception

Where Can I Find the C# Language Specification 6.0? [closed]

强颜欢笑 提交于 2019-12-17 17:33:54
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I know where to find the C# 5 Language Specification but I cannot find the C# 6 Language Specification anywhere. Where is the C# 6 Language Specification? 回答1: At time of writing (May 2016) Microsoft hasn't yet finished updating the spec for C#6. In the meantime, I put up Microsoft's latest current draft of the

TryParse with out var param

允我心安 提交于 2019-12-17 15:55:09
问题 A new feature in C# 6.0 allows to declare variable inside TryParse method. I have some code: string s = "Hello"; if (int.TryParse(s, out var result)) { } But I receive compile errors: What I am doing wrong? P.S.: in project settings C# 6.0 and .NET framework 4.6 are set. 回答1: A new feature in C# 6.0 allows to declare variable inside TryParse method. Declaration expressions was cut from C# 6.0 and wasn't shipped in the final release. You currently can't do that. There is a proposal for it on

Formatting a string into columns using String Interpolation

牧云@^-^@ 提交于 2019-12-17 14:54:57
问题 I need to print doubles so that definite number of symbols (like 8) is allocated for string representation of value. Next words should start at same index from beginning of string in each string. Now I have: value: 0 test value: 0.3333333333333 test value: 0.5 test I need: value: 0 test value: 0.33333333 test value: 0.5 test Test code: double[] ar = new double[] { 0, (double)1 / 3, (double)1 / 2 }; string s = "test"; foreach (var d in ar) { Console.WriteLine($"value: {d} {s}"); } What should

Why can't I use the null propagation operator in lambda expressions?

…衆ロ難τιáo~ 提交于 2019-12-17 10:46:11
问题 I often use null propagating operator in my code because it gives me more readable code, specially in long queries I don't have to null-check every single class that is used. The following code throws a compile error that we can't use null propagating operator in lambda. var cnt = humans.AsQueryable().Count(a => a.House?[0].Price == 5000); The error : Error CS8072 An expression tree lambda may not contain a null propagating operator. C# Could easily translate above code to the code to

C# 6.0 TFS Builds

房东的猫 提交于 2019-12-17 09:46:45
问题 I'm trialing the new features of C# 6.0 within Visual Studio 2015 CTP and my project is failing to build in TFS 2013 and Visual Studio Online. I understand that Visual Studio uses the new Roslyn compiler, which replaces the native .NET one, and the TFS build agent therefore is unable to compile. My question is how do I install Roslyn on the build agent (and within Visual Studio Online) and tell the build agent to use this compiler over native? 回答1: For the compilation step, you have a couple

Lambda for getter and setter of property

*爱你&永不变心* 提交于 2019-12-17 07:25:49
问题 In C# 6.0 I can write: public int Prop => 777; But I want to use getter and setter. Is there a way to do something kind of the next? public int Prop { get => propVar; set => propVar = value; } 回答1: First of all, that is not lambda, although syntax is similar. It is called "expression-bodied members". They are similar to lambdas, but still fundamentally different. Obviously they can't capture local variables like lambdas do. Also, unlike lambdas, they are accessible via their name:) You will

Expression-bodied function members efficiency and performance in C# 6.0

怎甘沉沦 提交于 2019-12-17 05:03:24
问题 In a new C# 6.0 we can define methods and properties using lambda expressions. For instance this property public string Name { get { return First + " " + Last; } } can be now defined as follows: public string Name => First + " " + Last; The information about expression-boided function members you can find here: http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx Does anyone know if there's any overhead when using new syntax? Can it slow down (or improve efficiency of

What is the difference between getter-only auto properties and expression body properties?

眉间皱痕 提交于 2019-12-17 04:58:43
问题 In the C# 6, you can can simplify implementing a property by using a getter-only auto property. For example, if I were implementing the abstract Stream class: public override bool CanRead { get; } = true; However I can also write it with an expression body, also new in C# 6: public override bool CanRead => true; What is the difference between the two, and when should I use one or the other? 回答1: They are syntactic sugar for two different things. The former initializes a backing field, and

Delegate caching behavior changes in Roslyn

南笙酒味 提交于 2019-12-17 04:32:25
问题 Given the following code: public class C { public void M() { var x = 5; Action<int> action = y => Console.WriteLine(y); } } Using VS2013, .NET 4.5. When looking at the decompiled code, we can see that the compiler is caching the delegate at the call site: public class C { [CompilerGenerated] private static Action<int> CS$<>9__CachedAnonymousMethodDelegate1; public void M() { if (C.CS$<>9__CachedAnonymousMethodDelegate1 == null) { C.CS$<>9__CachedAnonymousMethodDelegate1 = new Action<int>(C.<M