c#-6.0

What is the optional argument in C# interpolated string for?

北城余情 提交于 2019-11-30 00:15:48
问题 Interpolated strings is one of the new features of C# 6.0. According to MSDN, the syntax of the embedded C# expressions can contain an optional, comma-separated value, deemed as <optional-comma-field-width> in the documentation. Unfortunately I didn't find what this field is for. From its name one might think that this value sets the maximal size of the "interpolated" field, but when I try the following expression: var p = Process.GetCurrentProcess(); Console.WriteLine($"Process name is {p

Escaping Quotes inside new C# 6 String Syntax

断了今生、忘了曾经 提交于 2019-11-30 00:12:00
问题 I'm really excited about the new features in C# 6, including the new string syntax: var fullName = $"My Name is {FirstName} {LastName}"; However, I can't figure out how to escape quotes inside the braces to do the follow: bool includePrefix = true; var fullName = $"My name is {includePrefix ? "Mr. " : ""}{FirstName} {LastName}"; C# 6 doesn't like that. I've had to revert to using String.Format in that second case. Is it possible to escape quotes using the new syntax? Update: Yes, I have tried

What does “=>” operator mean in a property in C#? [duplicate]

徘徊边缘 提交于 2019-11-29 22:53:13
This question already has an answer here: What is the => assignment in C# in a property signature 5 answers What does this code mean? public bool property => method(); This is an expression-bodied property , a new syntax for computed properties introduced in C# 6, which lets you create computed properties in the same way as you would create a lambda expression. This syntax is equivalent to public bool property { get { return method(); } } Similar syntax works for methods, too: public int TwoTimes(int number) => 2 * number; stop-cran That's an expression bodied property. See MSDN for example.

Convert Contains To Expression Tree

不问归期 提交于 2019-11-29 18:11:22
Related To: Create a Lambda Expression With 3 conditions Please consider this Code: from a in myTbl where a.Address.Contains(strToCheck) select a How I can convert this to Expression Tree and write above code with Expressions? Main Problem is converting a.Address.Contains(strToCheck) to Expression Tree . Edit 1) Address is a string field and strToCheck is a string Thanks a.Address.Contains(strToCheck) represents a call to string.Contains instance method on a.Address instance with strToCheck argument . The simplest way to build the corresponding expression is to use the following Expression

C#6's new Collection Initializer - Clarification?

假装没事ソ 提交于 2019-11-29 16:58:16
问题 I've read that : The team have generally been busy implementing other variations on initializers. For example you can now initialize a Dictionary object But looking at : var Dic = new Dictionary<string,int>{ {"x",3}, {"y",7} }; VS var Dic = new Dictionary<string,int>{ ["x"]=3, ["y"]=7 }; I don't see where the benefit is. it looks the same. Both are nothing more than a name-value collection. They swapped pairs of curly braces for pairs of square brackets and some commas Question: What is the

What is the default culture for C# 6 string interpolation?

丶灬走出姿态 提交于 2019-11-29 15:56:33
问题 In C# 6 what is the default culture for the new string interpolation? I've seen conflicting reports of both Invariant and Current Culture. I would like a definitive answer and I'm keeping my fingers crossed for Invariant. 回答1: Using string interpolation in C# is compiled into a simple call to String.Format . You can see with TryRolsyn that this: public void M() { string name = "bar"; string result = $"{name}"; } Is compiled into this: public void M() { string arg = "bar"; string text = string

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

非 Y 不嫁゛ 提交于 2019-11-29 13:51:32
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 suggestion for one line code to make assignment valid would be appreciated. This code: Article? art will

What CLR is needed for C# 6?

女生的网名这么多〃 提交于 2019-11-29 13:22:02
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. 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 available to VS 2013 with addon https://github.com/dotnet/roslyn It is version 4 of CLR that is used. If you test

NullReferenceException with object initializer suggested by resharper

只愿长相守 提交于 2019-11-29 12:08:41
I have a strange issue with the object initializer syntax. Here are my sample classes: public class Foo { public Bah BahProp { get; set; } } public class Bah { public int Id { get; set; } } Consider following three ways to initialize an object: The old, verbose but explicit way, working correctly: var foo1 = new Foo(); foo1.BahProp = new Bah(); foo1.BahProp.Id = 1; // correctly initialized The second way i'm always using, using object initializer syntax: var foo2 = new Foo { BahProp = new Bah { Id = 1 } }; // correctly initialized A third way resharper has suggested my collegue with a

Property with private setter versus get-only-property

假装没事ソ 提交于 2019-11-29 11:05:17
问题 C# 6.0 introduces the ability to define get-only properties: public ICommand AddCommand { get; } Now when defining another property like the following, ReSharper suggests Auto-property can be made get-only : private List<Screenshot> Screenshots { get; set; } Futhermore, ReSharper doesn't say a thing when defining private getters: public ICommand AddCommand { get; private set; } What's the difference between a public get-only property (such as the first AddCommand ), a private get-only