Questions tagged [c#-8.0]

17

votes
2

answer
694

Views

What does null! statement mean?

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...
isxaker
-1

votes
1

answer
28

Views

C# 8.0 availability in Visual Studio 2017?

I'd like to use C# 8.0 (especially ranges and non-nullable reference types). I only found some information that there is a preview of it in the new VS2019 (even that is 2 months old now). Are there any plans that C# 8.0 is eventually also coming to VS17?
nonsensation
0

votes
0

answer
21

Views

Does default implementation of Interface members in C# 8 relates to overcome multiple inheritance problem of abstract class?

I'm reading this blog of .NET Engineering Team and they have introduced a new feature of default implementation for Interface. I'm confused related to its motive other than multiple level inheritance problem of abstract class. Other than that I'm not able to figure out any other benefit. Consdier th...
iSahilSharma
5

votes
2

answer
145

Views

IAsyncEnumerable<> broken in VS 2019 preview 2 (Core 3.0 preview 1)

After installing VS 2019 preview 2 i get a great number of errors. Error demo code: public class Class1 { public static async IAsyncEnumerable Get() { for( int i = 0; i < 10; i++ ) { await Task.Delay( 100 ); yield return i; } } } and nothig more (a new dll project)! With preview 1 was ok. The proj...
me-on-stov
131

votes
1

answer
5.8k

Views

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

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...
Michael Pittino
0

votes
2

answer
91

Views

Default interface methods and default values for Auto Properties

Given that an auto property compiles to a get_method, a set_method and a private variable and since C# 8 is introducing default interface methods Can properties in Interfaces have default implementations? Especially a get only property?
John Demetriou
1

votes
1

answer
109

Views

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

According to the anouncement of the new C# 8.0 feature named "nullable reference types" it can be enabled for the whole project. (mentioned here: https://youtu.be/VdC0aoa7ung?t=137) But how to enable it for the project? Did not find any new appropriate option in Project Properties window in Visual S...
dthpth
3

votes
3

answer
160

Views

C# 8 - multiple inheritance “abstract class”? [duplicate]

This question already has an answer here: Default Interface Implementations. What is deep meaningful difference now, between abstract class and interface? 3 answers It seems to me like the C# 8.0 feature, default interface member implementation, essentially allows one to create implementations at t...
Lasooch
1

votes
3

answer
123

Views

Does C# 8 annotate nullable properties and parameters?

I'm curious about how nullable reference types work, not in your own codebase, but with an already compiled library. Will C# be able to know if a property or parameter is nullable, perhaps checking the existance of some compiler-added attribute?
Max Toro
3

votes
1

answer
133

Views

Can new C# language features be used to to clean-up Task.WhenAll syntax?

With "async everywhere", the ability to fire off multiple heterogeneous operations is becoming more frequent. The current Task.WhenAll method returns its results as an array and requires all tasks to return the same kind of object which makes its usage a bit clumsy. I'd like to be able to write......
NeilMacMullen
3

votes
2

answer
1.2k

Views

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

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 actual method impl...
Marko
42

votes
8

answer
17.5k

Views

How would you implement a “trait” design-pattern in C#?

I know the feature doesn't exist in C#, but PHP recently added a feature called Traits which I thought was a bit silly at first until I started thinking about it. Say I have a base class called Client. Client has a single property called Name. Now I'm developing a re-usable application that will be...
mpen
4

votes
1

answer
750

Views

Nullable Reference Types with C# 8

Null reference exceptions are one of the top sources of program failures. Tony Hoare called it his billion dollar mistake. So I'm particularly looking forward to C# 8 and the new nullable reference types feature. I think I've got a pretty good grasp of the feature and what it's going to mean for my...
pmcilreavy
8

votes
2

answer
372

Views

Default Interface Methods in C# 8

Consider the following code example: public interface IPlayer { int Attack(int amount); } public interface IPowerPlayer: IPlayer { int IPlayer.Attack(int amount) { return amount + 50; } } public interface ILimitedPlayer: IPlayer { new int Attack(int amount) { return amount + 10; } } public class Pla...
Bassam Alugili
7

votes
3

answer
433

Views

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

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 al...
Tornike Gomareli
4

votes
3

answer
97

Views

C# 8 - multiple inheritance “abstract class”?

It seems to me like the C# 8.0 feature, default interface member implementation, essentially allows one to create implementations at the interface level. Pairing that with the fact that a class can implement multiple interfaces, it seems eerily close to a multiple inheritance structure for classes....
Lasooch
4

votes
2

answer
102

Views

Determine if type reference is nullable/non-nullable

Using the upcoming C# 8 nullable reference type feature, how can I tell if the type signature for a field/method/ property etc is a nullable or non-nullable reference type at runtime?
Craig Smitham