c#-8.0

Nullable reference types with generic return type

≡放荡痞女 提交于 2019-11-27 03:03:51
问题 I'm playing around a bit with the new C# 8 nullable reference types feature, and while refactoring my code I came upon this (simplified) method: public T Get<T>(string key) { var wrapper = cacheService.Get(key); return wrapper.HasValue ? Deserialize<T>(wrapper) : default; } Now, this gives a warning Possible null reference return which is logical, since default(T) will give null for all reference types. At first I thought I would change it to the following: public T? Get<T>(string key) But

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

烂漫一生 提交于 2019-11-27 02:45:56
问题 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

What does null! statement mean?

我只是一个虾纸丫 提交于 2019-11-26 12:24:43
问题 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