c#-6.0

How to upgrade msbuild to C# 6?

霸气de小男生 提交于 2019-11-26 18:00:47
问题 I want to use C# 6 in my project (null propagation, other features). I've installed VS 2015 on my PC and it works brilliantly and builds test code like var user = new SingleUserModel(); //all model fields are null var test = user.User?.Avatar?["blah"]; But when I push my project to the repo and CI starts to build it, build fails because of unsupported ? . I've installed VS2015 on CI server too but looke like it doesn't use it. What can I do? CI - CruiseControl .NET Builds with C:\Windows

How to implement INotifyPropertyChanged in C# 6.0?

ぐ巨炮叔叔 提交于 2019-11-26 17:46:11
问题 The answer to this question has been edited to say that in C# 6.0, INotifyPropertyChanged can be implemented with the following OnPropertyChanged procedure: protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } However, it isn't clear from that answer what the corresponding property definition should be. What does a complete implementation of INotifyPropertyChanged look like in C# 6.0 when

Is C# 6 ?. (Elvis op) thread safe? If so, how?

[亡魂溺海] 提交于 2019-11-26 16:49:13
问题 Apologies in advance: this question comes from a hard-core, unreformed C++ developer trying to learn advanced C#. Consider the following: if (myUserDefinedObject != null) { myUserDefinedObject.ToString(); } This is obviously not thread safe. On the other hand, I've seen two tutorials that say ?. (the Null Conditional Operator or 'Elvis Operator') for example, myUserDefinedObject?.ToString(); IS thread safe. Unless the compiler wraps a [mutex?] lock around it under the covers (shiver), I don't

What is the final format for string interpolation in VS 2015?

廉价感情. 提交于 2019-11-26 16:48:44
问题 I can't get string interpolation to work. Last news from MS I found was http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx However all that is said there is not working. Anyone knows if string interpolation made it into VS 2015? Is there any documentation about it? Can one you give an example? For instance, none of these formats work ( edited ): int i = 42; var s = "\{i}"; // correction after jon's answer: this works! var s = $"{i}"; // compiler error var s = "{{i}}

nameof expression in .net framework 4

泪湿孤枕 提交于 2019-11-26 16:33:16
问题 "nameof" expression is introduced in Visual Studio 2015 and c# 6 nameof (C# and Visual Basic Reference) How can u use it or write a similar method in older versions like .net framework 4. 回答1: If you're talking about an equivalent for C# before C#6, this will get the job done (in a hacky way) for properties. It can probably be expanded upon to include fields, methods, etc. public static class TestExtension { public static String nameof<T, TT>(this T obj, Expression<Func<T, TT>>

How to use the ternary operator inside an interpolated string?

喜欢而已 提交于 2019-11-26 15:52:55
I'm confused as to why this code won't compile: var result = $"{fieldName}{isDescending ? " desc" : string.Empty}"; If I split it up, it works fine: var desc = isDescending ? " desc" : string.Empty; var result = $"{fieldName}{desc}"; According to the documentation : The structure of an interpolated string is as follows: { <interpolationExpression>[,<alignment>][:<formatString>] } The problem is that the colon is used to denote formatting, like: Console.WriteLine($"The current hour is {hours:hh}") The solution is to wrap the conditional in parenthesis: var result = $"Descending {(isDescending ?

Automated property with getter only, can be set, why?

▼魔方 西西 提交于 2019-11-26 15:50:14
问题 I created an automated property: public int Foo { get; } This is getter only. But when I build a constructor, I can change the value: public MyClass(string name) { Foo = 5; } Why is it possible, even though this is get-only? 回答1: This is a new C# 6 feature, "Getter-only auto-properties", also known as "Auto-Property Initializers for Read-Only Properties" as discussed in this MSDN magazine article 'C# : The New and Improved C# 6.0' by Mark Michaelis and in the C# 6.0 draft Language

How to use C# 6 with Web Site project type?

送分小仙女□ 提交于 2019-11-26 15:25:14
问题 Updated an existing Web Site project type Visual Studio 2015, I changed the Framework to 4.6. I then expected to have all those new features available in my code behind files. Unfortunately I'm getting errors like: Error CS8026: Feature 'expression-bodied property' is not available in C# 5. Please use language version 6 or greater. or e.g.: Error CS8026: Feature 'interpolated strings' is not available in C# 5. Please use language version 6 or greater. I did a quick Google check and found a

String interpolation in a Razor view?

徘徊边缘 提交于 2019-11-26 15:23:10
问题 Is this supported? If so, is there some trick to enabling it? I'm assuming Razor isn't using a new enough compiler...? The VS2015 IDE seems to be fine with it but at runtime I am getting CS1056: Unexpected character '$' 回答1: Update : Starting in Visual Studio 2015 Update 1, there is a simple process in the GUI to do the steps below for you. Simply right-click your web project and select "Enable C# 6 / VB 14". More information is available on the MSDN blog post, "New feature to enable C# 6 /

What is the meaning of the planned “private protected” C# access modifier?

 ̄綄美尐妖づ 提交于 2019-11-26 15:10:00
问题 As part of the Roslyn documentation on GitHub, there's a page called Language feature implementation status, with planned language features for C# and VB. One feature I couldn't wrap my head around was private protected access modifier: private protected string GetId() { … } There is also a page of C# Language Design Notes, which explains many new features, but not this one. Eric Lippert said in a comment: Your error is in thinking of the modifiers as increasing restrictions. The modifiers in