c#-8.0

What is the difference between using and await using? And how can I decide which one to use?

放肆的年华 提交于 2019-12-30 09:38:14
问题 I've noticed that in some case, Visual Studio recommends to do this await using var disposable = new Disposable(); // Do something instead of this using var disposable = new Disposable(); // Do something What is the difference between using and await using ? How should I decide which one to use? 回答1: Classic sync using Classic using calls the Dispose() method of an object implementing the IDisposable interface. using var disposable = new Disposable(); // Do Something... Is equivalent to

How to deal with optional arguments when wanting to enable nullable reference types?

白昼怎懂夜的黑 提交于 2019-12-25 01:35:48
问题 I see the very great advantage of turning on (non-)nullable reference types, but I have quite a lot of methods with optional parameters and I am wondering what the right way is to correct the warnings yielded by the compiler in a wise way. Making the parameter nullable by annotating the type with "?" takes all the goodness away. Another idea is to turn all methods with optional parameters into separate methods which is quite a lot of work or yields high complexity (exponential explosion of

Use preview features & preview language in Visual Studio [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 10:49:25
问题 This question already has answers here : Does C# 8 support the .NET Framework? (3 answers) Closed 6 months ago . How can I turn on C# preview features in Visual Studio? The feature 'nullable reference types' is currently in Preview and "unsupported". To use Preview features, use the 'preview' language version. The project Properties does not offer "unsupported preview of next C# version (preview)" as an option: 回答1: Synopsis In order to turn on the preview features, you need all these: Visual

C#'s can't make `notnull` type nullable

亡梦爱人 提交于 2019-12-23 07:55:17
问题 I'm trying to create a type similar to Rust's Result or Haskell's Either and I've got this far: public struct Result<TResult, TError> where TResult : notnull where TError : notnull { private readonly OneOf<TResult, TError> Value; public Result(TResult result) => Value = result; public Result(TError error) => Value = error; public static implicit operator Result<TResult, TError>(TResult result) => new Result<TResult, TError>(result); public static implicit operator Result<TResult, TError>

If default interface methods are implemented in C# 8.0 why would I ever need abstract classes? [closed]

别说谁变了你拦得住时间么 提交于 2019-12-19 02:47:11
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . I recently ran into a list of features that are being considered for addition in the next version of C#. One of them is called "default interface methods": https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md In short, it will allow you to define

c# 8 switch expression multiple cases with same result

好久不见. 提交于 2019-12-18 05:45:12
问题 How does a switch-expression needs to be written to support multiple cases returning the same result? With c# prior to version 8 a switch may be written like so: var switchValue = 3; var resultText = string.Empty; switch (switchValue) { case 1: case 2: case 3: resultText = "one to three"; break; case 4: resultText = "four"; break; case 5: resultText = "five"; break; default: resultText = "unkown"; break; } When I am using the c# version 8 expression syntax its like so: var switchValue = 3;

How can I use C# 8 with Visual Studio 2017?

落爺英雄遲暮 提交于 2019-12-18 05:28:11
问题 I'd like to use C# 8.0 (especially ranges and non-nullable reference types) in Visual Studio 2017. Is it possible? 回答1: Going forward, Microsoft want to tie C# language versions more closely to framework versions than they have in the past. They really only want you to be using C# 8 with .NET Core 3.x and .NET Standard 2.1 projects, and that means using Visual Studio 2019. My answer to Does C# 8 support the .NET Framework? has all the gory details. However, if you really want to you can now

Does C# 8 support the .NET Framework?

你。 提交于 2019-12-17 04:26:39
问题 In Visual Studio 2019 Advanced Build settings, C# 8 does not appear to be available for a .NET Framework project, only (as in the picture below) for a .NET Core 3.0 project: Does C# 8 support the .NET Framework? 回答1: Yes, C# 8 can be used with the .NET Framework and other targets older than .NET Core 3.0/.NET Standard 2.1 in Visual Studio 2019 (or older versions of Visual Studio if you install a Nuget package). The language version must be set to 8.0 in the csproj file. Most - but not all -

Calling C# interface default method from implementing struct without boxing

不想你离开。 提交于 2019-12-12 14:57:19
问题 The only thing I can think of is as follows, which is far from ideal: interface IBar { void Foo() => Console.WriteLine("Hello from interface!"); } struct Baz : IBar { // compiler error void Test1() => this.Foo(); // IIRC this will box void Test2() => ((IBar)this).Foo(); // this shouldn't box but is pretty complicated just to call a method void Test3() { impl(ref this); void impl<T>(ref T self) where T : IBar => self.Foo(); } } Is there a more straightforward way to do this? (Related and how I

Calling C# interface default method from implementing class

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 04:24:56
问题 C# 8 supports default method implementations in interfaces. My idea was to inject a logging method into classes like this: public interface ILoggable { void Log(string message) => DoSomethingWith(message); } public class MyClass : ILoggable { void MyMethod() { Log("Using injected logging"); // COMPILER ERROR } } I get a compiler error: "The name does not exist in the current context" Is it impossible to use default method implementations in this way? EDIT: For the correct response regarding C