c#-6.0

Using null-conditional bool? in if statement [duplicate]

前提是你 提交于 2021-01-22 03:36:27
问题 This question already has answers here : Converting Nullable<bool> to bool (6 answers) Compiler Error for Nullable Bool (5 answers) cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?) (9 answers) Closed 3 years ago . Why this code works: if (list?.Any() == true) but this code doesn't: if (list?.Any()) saying Error CS0266 Cannot implicitly convert type 'bool?' to 'bool' So why is it not a language feature making such an implicit conversion

Using null-conditional bool? in if statement [duplicate]

落花浮王杯 提交于 2021-01-22 03:34:24
问题 This question already has answers here : Converting Nullable<bool> to bool (6 answers) Compiler Error for Nullable Bool (5 answers) cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?) (9 answers) Closed 3 years ago . Why this code works: if (list?.Any() == true) but this code doesn't: if (list?.Any()) saying Error CS0266 Cannot implicitly convert type 'bool?' to 'bool' So why is it not a language feature making such an implicit conversion

String interpolation inside string interpolation

依然范特西╮ 提交于 2021-01-20 12:31:09
问题 Is it possible to have a variable with a string format that you would like interpolated. public class Setting { public string Format { get; set; } } var setting = new Setting { Format = "The car is {colour}" }; var colour = "black"; var output = $"{setting.Format}"; Expected output "The car is black". 回答1: You can't do that. String interpolation is a purely compile-time feature. 回答2: No you can't do that, but you can achieve the same with a slightly different approach, that I've come to like:

Does C# 6 string interpolation use boxing like string.Format() does for its arguments?

那年仲夏 提交于 2020-12-05 07:04:26
问题 I am asking this for performance sake - using lots of boxing makes lots of heap allocations which brings more GC collects which sometimes causes apps to freeze for a glimpse which annoy users. 回答1: All string interpolation does (at least in the common case) is to call string.Format() . Right now, calling string.Format() allocates quite a lot and not just due to boxing (for example, string.Format("{0:s} - {1:B}: The value is: {2:C2}", DateTime.UtcNow, Guid.NewGuid(), 3.50m) makes 13

C# 6.0 - Unexpected character '$' [closed]

别等时光非礼了梦想. 提交于 2020-07-03 06:13:43
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . Improve this question I'm new to programming and I have just installed Visual Studio 2017. I created this code (from the book I'm learning), but this does not compile. I have problem with string interpolation and I get error: Unexpected character '$', but I'm using C# 6.0 so this

Method chains with nameof operator

﹥>﹥吖頭↗ 提交于 2020-04-07 04:26:45
问题 If I do nameof(instance.SomeProperty) , it evaluates to "SomeProperty" . Is there any way I can get the entire method chain "instance.SomeProperty" ? I know I could do nameof(instance) + "." + nameof(instance.SomeProperty) , but is there a better way that's more maintainable? 回答1: Is there any way I can get the entire method chain "instance.SomeProperty"? Nope. You can, however, do something similar to your other solution: $"{nameof(instance)}.{nameof(instance.SomeProperty)}" You can try it

Get windows user password in a Windows Forms application

不羁的心 提交于 2020-02-07 05:18:05
问题 I need to get the password from the user that is logged on on Windows. I need this information as string for a Windows Forms application. is there any way of doing that with LDAP, SSO, external dll or similar? Thanks a lot! 回答1: Windows does not store the password as plain text. The password is stored hashed with NTLMv2 and therefor highly encrypted. It is not possible to reverse engineer that password or get it. As Lucax said it would be a huge security issue if every program could read the

Is there any way for the nameof operator to access method parameters (outside of the same method)?

血红的双手。 提交于 2020-02-01 09:52:25
问题 Take the following class and method: public class Foo public Foo Create(string bar) { return new Foo(bar); } So getting "Create" is obvious: nameof(Foo.Create) Is there any way to get "bar" other than using reflection to read the parameters at run time? 回答1: No. There is no way to get the parameter names from the outside of the method using nameof . nameof doesn't work for method parameters if you want the name on the calling side (for the callee it does work obviously). The other methods you

Interpolated string formatting issue

亡梦爱人 提交于 2020-01-30 04:21:14
问题 I have stumbled upon one issue with interpolated strings for a several times now. Consider the following case: double number = 123.4567; var str = $"{{{number:F2}}}"; //I want to get "{123.45}" Console.WriteLine(str); // Will print "{F2}" A little surprising at first but once you realize how the curly brackets are paired it makes sense. Two following curly brackets are an escape sequence for a single curly in the interpolated string. So the opening bracket of the interpolated expression is

Enabling C# 6 in ASP.VNext projects in Visual Studio CTP2

旧时模样 提交于 2020-01-23 06:46:28
问题 I have installed Visual Studio CTP2 and created a new ASP.Net Vext project. When I tried using C# 6.0 features, it was not working. I even tried the stpes in the following link. No C# 6.0 in Visual Studio 2015 CTP? But even after this I was not able to use C# 6 in VNext projects. Please help. 回答1: Add this to your project.json: "compilationOptions": { "languageVersion": "experimental" } 回答2: You should not add the net451 object. Use this inside the project.json file: { "compilationOptions": {