c#-8.0

IAsyncEnumerable not working in C# 8.0 preview

自闭症网瘾萝莉.ら 提交于 2019-12-05 20:54:24
问题 I was playing around with C# 8.0 preview and can't get IAsyncEnumerable to work. I tried the following public static async IAsyncEnumerable<int> Get() { for(int i=0; i<10; i++) { await Task.Delay(100); yield return i; } } I ended up using a Nuget package named AsyncEnumerator , but I'm getting the following error: Error CS1061 ' IAsyncEnumerable<int> ' does not contain a definition for ' GetAwaiter ' and no accessible extension method ' GetAwaiter ' accepting a first argument of type '

How to detect whether a type can be nullable at runtime?

元气小坏坏 提交于 2019-12-05 02:09:20
问题 I'm trying to detect whether a type can be nullable or not at runtime to convert that type to the corresponding GraphQL type so that, for example: With nullable reference types enabled : string is converted to String! string? is converted to String With nullable reference types disabled : string is converted to String NonNull<string> is converted to String! ( NonNull is a custom library type) I'm having trouble adapting the code that detected the nullability of a type: bool isNullable =

IAsyncEnumerable not working in C# 8.0 preview

柔情痞子 提交于 2019-12-04 03:15:57
I was playing around with C# 8.0 preview and can't get IAsyncEnumerable to work. I tried the following public static async IAsyncEnumerable<int> Get() { for(int i=0; i<10; i++) { await Task.Delay(100); yield return i; } } I ended up using a Nuget package named AsyncEnumerator , but I'm getting the following error: Error CS1061 ' IAsyncEnumerable<int> ' does not contain a definition for ' GetAwaiter ' and no accessible extension method ' GetAwaiter ' accepting a first argument of type ' IAsyncEnumerable<int> ' could be found (are you missing a using directive or an assembly reference?) Error

How to detect whether a type can be nullable at runtime?

别来无恙 提交于 2019-12-03 21:08:15
I'm trying to detect whether a type can be nullable or not at runtime to convert that type to the corresponding GraphQL type so that, for example: With nullable reference types enabled : string is converted to String! string? is converted to String With nullable reference types disabled : string is converted to String NonNull<string> is converted to String! ( NonNull is a custom library type) I'm having trouble adapting the code that detected the nullability of a type: bool isNullable = !typeInfo.IsValueType; How can I change it so that it works with nullable reference types both enabled and

Cannot use interface default methods

夙愿已清 提交于 2019-12-02 02:36:29
问题 I have installed new version of visual studio and set the project to C# 8. When I tried to use new interface feature like below it said that I cannot implement in interface which seems that somehow I cannot use new feature. Can anyone explain why, is it still not included in 2019 preview? interface IDefaultInterfaceMethod { public void DefaultMethod() { Console.WriteLine("I am a default method in the interface!"); } } 回答1: Based on this: Microsoft has fleshed out more details about C# Version

Non-nullable string type, how to use with Asp.Net Core options

大城市里の小女人 提交于 2019-12-01 01:28:20
问题 MS states Express your design intent more clearly with nullable and non-nullable reference types. My intent is to express, that properties Issuer and Audience in my JwtOptions are never null. Which is very reasonable intent for consumers of these options, is not? These not null values are ensured by Asp.Net Core validation described below. But if JwtOptions has not all properties initialized by the constructor, so C# 8 compiler reports Warning CS8618 Non-nullable property 'Issuer' is

Default Interface Implementations. What is deep meaningful difference now, between abstract class and interface?

跟風遠走 提交于 2019-11-30 09:17:35
I know that an abstract class is a special kind of class that cannot be instantiated. An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards. Also I know that An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature

What does null! statement mean?

与世无争的帅哥 提交于 2019-11-28 18:33:01
I've recently seen the following code: public class Person { //line 1 public string FirstName { get; } //line 2 public string LastName { get; } = null!; //assign null is possible public string? MiddleName {get; } = null; public Person(string firstName, string lastName, string middleName) { FirstName = firstName; LastName = lastName; MiddleName = middleName; } public Person(string firstName, string lastName) { FirstName = firstName; LastName = lastName; MiddleName = null; } } Basically I try to dig into new c# 8 features. One of them is NullableReferenceTypes . Actually there're a lot of

Why doesn't the new hat-operator index from the C# 8 array-slicing feature start at 0?

白昼怎懂夜的黑 提交于 2019-11-28 16:54:00
问题 C# 8.0 introduces a convenient way to slice arrays - see official C# 8.0 blogpost. The syntax to access the last element of an array is int value[] = { 10, 11, 12, 13 }; int a = value[^1]; // 13 int b = value[^2]; // 12 I'm wondering why the indexing for accessing the elements backwards starts at 1 instead of 0? Is there a technical reason for this? 回答1: Official answer For better visibility, here is a comment from Mads Torgersen explaining this design decision from the C# 8 blog post: We

How to enable Nullable Reference Types feature of C# 8.0 for the whole project

扶醉桌前 提交于 2019-11-27 12:41:31
问题 According to the C# 8 announcement video the "nullable reference types" feature can be enabled for the whole project. But how to enable it for the project? I did not find any new appropriate option in the Project Properties window in Visual Studio 2019 Preview 1. Can it be enabled for 'legacy' .csproj projects if the C# language version is changed to 8.0? 回答1: In Visual Studio 16.2 (from preview 1) the property name changed to Nullable which is simpler and aligns with the command line