c#-6.0

Why can't I use the conditional operator in an interpolated string without brackets? [duplicate]

放肆的年华 提交于 2019-12-20 03:27:13
问题 This question already has an answer here : How to use the ternary operator inside an interpolated string? (1 answer) Closed 2 years ago . Why can't I use the inline conditional-operator inside a c#-6 string interpolation, without encompassing it within brackets? and the errors: As you can see, it appears the parser is having a hard time. Is this a bug, or a feature of the string interpolation mechanism? 回答1: From MSDN (emphasis mine): $"{person.Name, 20} is {person.Age:D3} year {(p.Age == 1 ?

How do I compile for .NET 2.0 with C# 6.0?

萝らか妹 提交于 2019-12-20 02:15:51
问题 Visual Studio 2015 has no problem compiling for the older CLR with new c# compiler. It seems that it uses VBCSCompiler.exe for this under the hood, but I cannot find any documentation about VBCSCompiler.exe command line options. On the other hand csc.exe does not seem to have an option to select target CLR. You can use the latest csc.exe which will compile for CLR 4, or you can use an older csc.exe to compile for CLR 2, but then it won't be C# 6. So how do I compile for CLR 2 and c# 6.0? Do I

How to specify the equivalent of /features:strict (of csc.exe) to msbuild.exe or in the .csproj file?

旧街凉风 提交于 2019-12-19 19:33:20
问题 Introduction Consider this simple (and bad) C# class: using System; namespace N { static class C { static void M(DateTime d) { if (d == null) Console.WriteLine("Yes"); else Console.WriteLine("No"); } static void L(object o) { if (o is Nullable) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } } Both methods M and L have serious issues. In M , we ask if a value of the non-nullable struct DateTime is equal to null via the lifted == operator (which exists since DateTime overloads

Why is nameof(object) not allowed?

无人久伴 提交于 2019-12-19 17:42:38
问题 In C# 6.0 you can write this: var instance = default(object); var type = typeof(object); They have the same result of: var instance = default(System.Object); var type = typeof(System.Object); But you can't write this: var name = nameof(object); It generates the following error: Invalid expression term 'object'. But you can still write this: var name = nameof(System.Object); Why nameof(object) does not compile? 回答1: The difference is that object is a synonym for the class Object and nameof()

Difference between (auto) properties initialization syntax in C# 6

[亡魂溺海] 提交于 2019-12-19 06:03:03
问题 What is the difference between the following expressions for initializing the properties in C# 6: 1. Auto-Property initialized from constructor public class Context1 { public Context1() { this.Items = new List<string>(); } public List<string> Items { get; private set; } } 2: Property initialized from a backing field public class Context2 { private readonly List<string> items; public Context2() { this.items = new List<string>(); } public List<string> Items { get { return this.items; } } } 3:

NullReferenceException with object initializer suggested by resharper

为君一笑 提交于 2019-12-18 06:58:56
问题 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

null conditional operator not working with nullable types?

房东的猫 提交于 2019-12-18 03:00:23
问题 I'm writing a piece of code in c#6 and for some strange reason this works var value = objectThatMayBeNull?.property; but this doesn't: int value = nullableInt?.Value; By not works I mean I get a compile error saying Cannot resolve symbol 'Value' . Any idea why the null conditional operator ?. isn't working? 回答1: Okay, I have done some thinking and testing. This is what happens: int value = nullableInt?.Value; Gives this error message when compiling: Type 'int' does not contain a definition

No C# 6.0 in Visual Studio 2015 CTP?

ⅰ亾dé卋堺 提交于 2019-12-17 22:46:31
问题 I have just created a new VM on Azure (using the image provided by the Azure team from the gallery) with CTP version of the upcoming Visual Studio 2014 that appeared yesterday online to test it and especially to play around with new C# 6.0 features. However, it does not provide support for C# 6.0 out of the box, for none of the language extensions (primary constructors, property initializers, safe navigation operator) work there. A blog entry on MSDN suggests adding the following to project

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

北城以北 提交于 2019-12-17 22:19:11
问题 This question already has answers here : What is the => assignment in C# in a property signature (6 answers) Closed 3 years ago . What does this code mean? public bool property => method(); 回答1: 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

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

让人想犯罪 __ 提交于 2019-12-17 20:42:43
问题 This question already has answers here : Difference in C# between different getter styles (3 answers) Closed 2 years ago . 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