c#-6.0

Using the Null Conditional Operator to check values on objects which might be null

时间秒杀一切 提交于 2019-12-08 14:41:39
问题 I've been playing with C# 6's Null Conditional Operator (more info here). I really like the syntax and I think it makes the code much more readable however I think it is questionable as to what exactly the code is going to do when you come across checking the value of a property on an object which itself might be null. For example, if I had a class with a decimal property on it and I wanted a conditional check on the value of that decimal, I would write something like: if (foo?.Bar > max) { /

What happens if the filter of an Exception filter throws an exception

谁说胖子不能爱 提交于 2019-12-08 14:28:33
问题 I have not worked in C# 6 yet but was wondering.... As the title says "What happens if the filter of an Exception filter throws an exception?". I guess the really answer is "The filter should be written in such a way that it never throws an exception.", but lets say it does. Will it be as if the exception happened inside the catch itself? try { throw new Exception("Forced Exception"); } catch (Exception ex) if (MethodThatThrowsAnException()) { WriteLine("Filtered handler 1"); } catch

Specify decimal places using variables inside string interpolation

蓝咒 提交于 2019-12-07 07:05:22
问题 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'

Why does the IL set this value twice?

爷,独闯天下 提交于 2019-12-07 01:24:41
问题 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

Auto-property initializer Singleton implementation

断了今生、忘了曾经 提交于 2019-12-06 22:46:44
问题 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

Why is declaration expression dropped in C# 6?

核能气质少年 提交于 2019-12-06 21:34:32
问题 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? 回答1: 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

How is C# string interpolation compiled?

你。 提交于 2019-12-06 18:30:06
问题 I know that interpolation is syntactic sugar for string.Format() , but does it have any special behavior/recognition of when it is being used with a string formatting method? If I have a method: void Print(string format, params object[] parameters) And the following call to it using interpolation: Print($"{foo} {bar}"); Which of the following calls lines is most equivalent to the compiled result of string interpolation? Print(string.Format("{0} {1}", new[] { foo, bar })); Print("{0} {1}", new

Explicit implementation of an interface using a getter-only auto-property (C# 6 feature)

青春壹個敷衍的年華 提交于 2019-12-06 16:51:14
问题 Using automatic properties for explicit interface implementation was not possible in C# 5, but now that C# 6 supports getter-only auto-properties, this should be possible now, right? Creating the auto-property succeeds in C# 6, but when trying to assign a value to it in the constructor, you have to cast this to the interface type first, since the implementation is explicit. But that's where both VS 2015 RC and VS Code 0.3.0 display the error that can be seen in the comment: using static

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

亡梦爱人 提交于 2019-12-06 03:56:17
问题 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

Which of one from string interpolation and string.format is better in performance?

南楼画角 提交于 2019-12-05 18:51:51
问题 Consider this code: var url = "www.example.com"; String.Format: var targetUrl = string.Format("URL: {0}", url); String Interpolation: var targetUrl=$"URL: {url}"; Which of one from string interpolation and string.Format is better in performance? Also what are the best fit scenarios for use of them? 回答1: Which of one from string interpolation and string.format is better in performance? Neither of them is better since they are equal on run-time. String interpolation is rewritten by the compiler