c#-6.0

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

僤鯓⒐⒋嵵緔 提交于 2019-11-27 14:35:51
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 understand how that can be true. If this idiom is thread safe, can someone point me to a technical

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

落爺英雄遲暮 提交于 2019-11-27 14:34:34
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}}"; // no interpolation edit about VS 2015 CTP 6 (20.4.2015 ) The final version is var s = $"{i}" also

nameof expression in .net framework 4

一世执手 提交于 2019-11-27 13:57:58
"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. 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>> propertyAccessor) { if (propertyAccessor.Body.NodeType == ExpressionType.MemberAccess) { var memberExpression =

Why can't I use the null propagation operator in lambda expressions?

試著忘記壹切 提交于 2019-11-27 12:31:55
I often use null propagating operator in my code because it gives me more readable code, specially in long queries I don't have to null-check every single class that is used. The following code throws a compile error that we can't use null propagating operator in lambda. var cnt = humans.AsQueryable().Count(a => a.House?[0].Price == 5000); The error : Error CS8072 An expression tree lambda may not contain a null propagating operator. C# Could easily translate above code to the code to following code if really can't do anything else! var cnt = humans.AsQueryable().Count(a => a.House != null &&

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

浪尽此生 提交于 2019-11-27 12:15:52
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? 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 Specification . The read-only field's setter is only accessible in the constructor, in all other scenarios the field is

How to upgrade msbuild to C# 6?

风格不统一 提交于 2019-11-27 11:24:10
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\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe jessehouwing Make sure you call: C:\Program Files (x86)

String interpolation in a Razor view?

倖福魔咒の 提交于 2019-11-27 11:10:08
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 '$' 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 / VB 14 ". Since this answer was written, this functionality has been added with the assistance of a NuGet

Error CS1056: Unexpected character '$' running the msbuild on a tfs continuous integration process

╄→尐↘猪︶ㄣ 提交于 2019-11-27 11:09:51
问题 I have a project that the framework is targeting .NET Framework 4.6.1 , as part of the continuous integration process on the tfs we created a Build Solution task to ensure that the code compiles correctly. Now the TFS server has the latest version of the .Net Famework 4.6.2 . On the register this is the value for the Release key of the framework On all other OS versions: 394806 => .NET Framework 4.6.2 But when the build runs it comes with this error: Error CS1056: Unexpected character '$' I

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

微笑、不失礼 提交于 2019-11-27 10:24:29
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 fact always decrease restrictions. Remember, things are "private" by default; only by adding

C# 6.0 TFS Builds

扶醉桌前 提交于 2019-11-27 08:53:19
I'm trialing the new features of C# 6.0 within Visual Studio 2015 CTP and my project is failing to build in TFS 2013 and Visual Studio Online . I understand that Visual Studio uses the new Roslyn compiler, which replaces the native .NET one, and the TFS build agent therefore is unable to compile. My question is how do I install Roslyn on the build agent (and within Visual Studio Online) and tell the build agent to use this compiler over native? For the compilation step, you have a couple of options: You can reference the Microsoft.Net.Compilers NuGet package on a per-project basis to use that