c#-6.0

Difference between => constant to { get; } = constant [duplicate]

橙三吉。 提交于 2019-11-28 13:14:20
This question already has an answer here: Difference in C# between different getter styles 3 answers In the context of best practice and performance (if any) what is better for exposing a value that is either set or calculated once as a property in C# 6+ style properties? I'm comparing expression bodied properties public string Name => "bob"; and auto-property initialisation public string Name { get; } = "bob"; Does it desugar to the same thing? I can't find anywhere in the docs that says which to use for my case. I apologise if this is covered already in SO, the search got me no where. Beware

C# 6.0 Null Propagation Operator & Property Assignment

江枫思渺然 提交于 2019-11-28 11:53:11
This question has been completely overhauled in the interest of being thorough in explanation. I have noticed what appears to be quite a poor limitation of the null propagation operator in C# 6.0 in that you cannot call property setters against an object that has been null propagated (though you can call property getters against an object that has been null propagated). As you will see from the generated IL (which I have reflected into C#) , there is nothing that should limit the ability to call property setters using null propagation. To start with, I have created a simple class, with both

Why C# 6.0 doesn't let to set properties of a non-null nullable struct when using Null propagation operator?

徘徊边缘 提交于 2019-11-28 07:22:04
问题 Assume we have following code : struct Article { public string Prop1 { get; set; } } Article? art = new Article(); art?.Prop1 = "Hi"; // compile-error The compile error is CS0131 The left-hand side of an assignment must be a variable, property or indexer. Actually art?.Prop1 is a property and should be considered as a valid assignment! I don't see any problem with assignment to make this code invalid. Why C# 6.0 doesn't let to set properties of a non-null nullable struct ? Alternately any

What is the difference between MyEnum.Item.ToString() and nameof(MyEnum.Item)?

▼魔方 西西 提交于 2019-11-28 07:21:03
问题 MyEnum.Item.ToString(); nameof(MyEnum.Item); Which style is preferred? Is there any practical difference between the two? 回答1: The first is a run-time call that will realise at runtime it needs to return the string "Item" , and do so. The second is another way to write "Item" straight into the code. The second would be slightly faster, but prior to C#6 would not have been available. To put "Item" in the code manually would have therefore been an optimisation that risked an error, while nameof

Read-Only Property in C# 6.0

早过忘川 提交于 2019-11-28 07:17:35
问题 Microsoft introduce a new syntax in C#6 that let you set your property to read-only as below: public class Animal { public string MostDangerous { get; } = "Mosquito"; } I am wondering what is the added value of such approach. What is the difference by just writing: public class Animal { public const string MostDangerous = "Mosquito"; } or even: public class Animal { public string MostDangerous { get { return "Mosquito"; } } } 回答1: Your example is using string constants which can't show all

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

萝らか妹 提交于 2019-11-28 07:13:57
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? The Exception Filters feature in C# 6.0 provides various benefits. Here's an explanation of some (ordered by my perceived

What CLR is needed for C# 6?

限于喜欢 提交于 2019-11-28 06:58:17
问题 The title says it all: what CLR version is / will be needed to run C# 6 programs? The CLR version is interesting to find out the system requirements and supported operating systems. I googled [1] [2] and had a look at Wikipedia and MSDN but could not find the information. 回答1: C # 6 language enhancements is compatible to .net framework starting from 2.0 to 4.6. It does not require any higher version of .net framework but need higher version of Visual studio such as VS 2015. C# 6 is also

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

∥☆過路亽.° 提交于 2019-11-28 04:02:20
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? 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 C#6 spec here: https://github.com/ljw1004/csharpspec/blob/gh-pages/README.md This current draft is almost finished, save for a few remaining TODO comments and checking. (This version has been converted into github+markdown, but it also has links to download DOCX and PDF versions of the spec,

Roslyn and .NET Runtime version [duplicate]

自古美人都是妖i 提交于 2019-11-28 02:32:11
问题 This question already has answers here : Does C# 6.0 work for .NET 4.0? (5 answers) Closed 3 years ago . Is it possible to use Roslyn compiler and new features of C# 6.0 with old versions of .NET Runtime (for example, .NET 4.0)? For example, I want use the expression-bodied members ( int S => x + y; instead of int S { get { return x + y; } } ) in .NET 4.0 application. 回答1: The new C# 6.0 features don't depend upon framework support so yes, you an app compiled with the C# 6.0 compiler will run

What benefits does dictionary initializers add over collection initializers?

强颜欢笑 提交于 2019-11-27 23:24:41
In a recent past there has been a lot of talk about whats new in C# 6.0 One of the most talked about feature is using Dictionary initializers in C# 6.0 But wait we have been using collection initializers to initialize the collections and can very well initialize a Dictionary also in .NET 4.0 and .NET 4.5 (Don't know about old version) like Dictionary<int, string> myDict = new Dictionary<int, string>() { { 1,"Pankaj"}, { 2,"Pankaj"}, { 3,"Pankaj"} }; So what is there new in C# 6.0, What Dictionary Initializer they are talking about in C# 6.0 While you could initialize a dictionary with