c#-6.0

Which “C# Experimental language feature” is this?

て烟熏妆下的殇ゞ 提交于 2019-12-04 03:03:36
问题 In the example below, Resharper shows "C# Experimental language feature" tooltip on the first curly bracket. I've checked the new features of C# 6.0 but didn't come across a similar one. What is the referred experimental feature? class Class1 { { // <= C# Experimental language feature } } Note: It is an error for .Net Framework 4.5 compiler. "Invalid token '{' in class, struct, or interface member declaration" 回答1: This was for Primary Constructors, a feature which has now been cut from C#6.

Overloaded string methods with string interpolation

六月ゝ 毕业季﹏ 提交于 2019-12-04 01:43:18
Why does string interpolation prefer overload of method with string instead of IFormattable ? Imagine following: static class Log { static void Debug(string message); static void Debug(IFormattable message); static bool IsDebugEnabled { get; } } I have objects with very expensive ToString() . Previously, I did following: if (Log.IsDebugEnabled) Log.Debug(string.Format("Message {0}", expensiveObject)); Now, I wanted to have the IsDebugEnabled logic inside Debug(IFormattable) , and call ToString() on objects in message only when necessary. Log.Debug($"Message {expensiveObject}"); This, however,

TeamCity Build using C#6

ぃ、小莉子 提交于 2019-12-04 01:18:57
问题 We use TeamCity (9.0.1) as our build server, but having recently upgraded our ASP.NET MVC solution to use VS2015 and C#6 syntax, I now get the following error message on our build server: Can not start build runner and more specifically: C:\TeamCity\buildAgent\work\86ee61c6c333dc3d\MyApplication\MyApplication.csproj.metaproj : error MSB4025: The project file could not be loaded. Could not find file 'C:\TeamCity\buildAgent\work\86ee61c6c333dc3d\MyApplication\MyApplication.csproj.metaproj'. I

nameof with Generics

蹲街弑〆低调 提交于 2019-12-03 18:00:33
问题 I was experimenting with nameof with generics. I didn't get the result I was expecting. I'm not sure if this is part of the spec or not. class MainClass { public static void Main (string[] args) { Console.WriteLine ($"Hello { nameof(FooBar<string>)! }"); } } class FooBar<T> { } The output I get is Hello FooBar! I would expect some details about the type parameters. I tried it with a method and that fails with a compiler error: class MainClass { public static void Main (string[] args) {

Interpolate string c# 6.0 and Stylecop

末鹿安然 提交于 2019-12-03 15:01:33
问题 I am using Stylecop version : 4.7.49.0 Has anyone used the latest interpolate string functionality in c# 6.0 example var totalUnits = GetUnitsGetTotalIssuedShares(myId); var testString = $"Test Units :{totalUnits}, have been shipped."; When I build i get the stylecop error SA0102 - because stylecop cant parse the file. It doesn't seem like there is a new version of stylecop that can handle 6.0 yet? error :SA0102: A syntax error has been discovered in file Is there anyway around this error?

using statement with static class is not working in visual studio 2015 CTP

廉价感情. 提交于 2019-12-03 11:58:42
I have written a following code for one of my C# 6.0 Sample application. It was working fine earlier with Visual Studio 2015 Preview. But now when I have downloaded the newest version of Visual Studio 2015 which launched before some time.( http://blogs.msdn.com/b/bharry/archive/2015/01/16/visual-studio-2015-ctp-5-is-available.aspx ) it stopped working. Following is a code for that. using System.Console; namespace StaticClassUsing { class Program { static void Main(string[] args) { WriteLine("With using statement"); } } } Now when you run this application in Latest Version of Visual Studio 2015

Null-conditional operator evaluates to bool not to bool? as expected

安稳与你 提交于 2019-12-03 10:54:43
问题 I've just upgraded from VS 2010 to 2015. I like the new null-conditional operator which is also known as null-propagation. This enables to simplify your code, for example: string firstCustomerName = customers?[0].Name; // null if customers or the first customer is null another one: int? count = customers?[0]?.Orders?.Count(); // null if customers, the first customer, or Orders is null which returns a Nullable<int> even if Enumerable.Count returns an int to differentiate between a valid count

Omitted setter vs private setter?

天大地大妈咪最大 提交于 2019-12-03 10:23:10
What is the difference between a property with a omitted setter and a property with a private setter? public string Foo { get; private set; } vs public string Foo { get; } In C# 6, get; only properties are only settable from the constructor. From everywhere else, it is read-only. A property with a private set; can be set from everywhere inside that class. Thomas Ayoub From outside the class, it won't change anything if you use this syntax: public string Foo { get; } But you won't be able to update Foo within the class, except in the constructor, to do so, you'll need the private setter: public

Null propagation operator and dynamic variable

China☆狼群 提交于 2019-12-03 10:14:44
I have been looking at the null-propagation operator in C#6 and tried to make it work with the variables of dynamic type but without success. Consider the code below, it compiles but CLR throws AccessViolationException at runtime when the null-propagation is applied to dynamic object. class SomeType { public object SomeProperty { get; set; } static void Main() { var obj = new SomeType() { SomeProperty = "ABCD" }; var p1 = ((dynamic)obj).SomeProperty; //OK, p1 is set to "ABCD" var p2 = ((dynamic)obj)?.SomeProperty; //AccessViolationException Console.ReadLine(); } } At first I thought that this

Trying to understand ?. (null-conditional) operator in C#

北城余情 提交于 2019-12-03 09:17:11
I have this very simple example: class Program { class A { public bool B; } static void Main() { System.Collections.ArrayList list = null; if (list?.Count > 0) { System.Console.WriteLine("Contains elements"); } A a = null; if (a?.B) { System.Console.WriteLine("Is initialized"); } } } The line if (list?.Count > 0) compiles perfectly which means that if list is null , the expression Count > 0 becomes false by default. However, the line if (a?.B) throws a compiler error saying I can't implicitly convert bool? to bool . Why is one different from the other? Heinzi list?.Count > 0 : Here you compare