c#-8.0

Nullable reference types unexpected CS8629 Nullable value type may be null with temporary variables

纵饮孤独 提交于 2021-02-18 22:45:43
问题 In a C# 8 project, I am using nullable reference types and am getting an unexpected (or at least, unexpected to me) CS8629 warning, bool singleContent = x.DataInt != null; bool multiContent = x.DataNvarchar != null; if (singleContent && multiContent) { throw new ArgumentException("Expected data to either associate a single content node or " + "multiple content nodes, but both are associated."); } if (singleContent) { var copy = x.DataInt.Value; // CS8629 here newPropertyData.DataNvarchar = $

Can I foreach over an array of structs without copying the elements in C# 8?

混江龙づ霸主 提交于 2021-02-08 14:49:22
问题 With the new readonly instance member features in C# 8, I try to minify unnecessary copying of struct instances in my code. I do have some foreach iterations over arrays of structs, and according to this answer, it means that every element is copied when iterating over the array. I thought I can simply modify my code now to prevent the copying, like so: // Example struct, real structs may be even bigger than 32 bytes. struct Color { public int R; public int G; public int B; public int A; }

Mark strings as non-nullable in ASP.NET Core 3.0 Swagger

微笑、不失礼 提交于 2021-02-07 13:14:51
问题 I'm using ASP.NET Core 3 and Swashbuckle with mostly default configuration and I have a DTO parameter with a string on it that I want to be non-nullable. How can I achieve this? Note, Required and nullability are separate concerns in Swagger. It's also using C#8 and the non-nullable stuff, so the compiler should be annotating the property as non-nullable already. It's understandable that Swashbuckle hasn't been updated to take that into account (and maybe can't) but I would like to be able to

What's the difference between returning AsyncEnumerable with EnumeratorCancellation or looping WithCancellation

时光毁灭记忆、已成空白 提交于 2021-02-07 05:53:46
问题 I have the following method that reads a csv document from a http stream public async IAsyncEnumerable<Line> GetLines([EnumeratorCancellation] CancellationToken cancellationToken) { HttpResponseMessage response = GetResponse(); using var responseStream = await response.Content.ReadAsStreamAsync(); using var streamReader = new StreamReader(responseStream); using var csvReader = new CsvReader(streamReader); while (!cancellationToken.IsCancellationRequested && await csvReader.ReadAsync()) {

Can a C# class call an interface's default interface method from its own implementation?

China☆狼群 提交于 2021-02-05 07:36:36
问题 If I have a default interface method like this: public interface IGreeter { void SayHello(string name) => System.Console.WriteLine($"Hello {name}!"); } Can I have my concrete implementation call that default method? public class HappyGreeter : IGreeter { public void SayHello(string name) { // what can I put here to call the default method? System.Console.WriteLine("I hope you're doing great!!"); } } So that calling: var greeter = new HappyGreeter() as IGreeter; greeter.SayHello("Pippy");

Can a C# class call an interface's default interface method from its own implementation?

谁说我不能喝 提交于 2021-02-05 07:36:27
问题 If I have a default interface method like this: public interface IGreeter { void SayHello(string name) => System.Console.WriteLine($"Hello {name}!"); } Can I have my concrete implementation call that default method? public class HappyGreeter : IGreeter { public void SayHello(string name) { // what can I put here to call the default method? System.Console.WriteLine("I hope you're doing great!!"); } } So that calling: var greeter = new HappyGreeter() as IGreeter; greeter.SayHello("Pippy");

Visual Studio Recommending to Simplify Using Command

会有一股神秘感。 提交于 2021-02-04 21:19:30
问题 I've noticed since updating Visual Studio that it is now recommending to simplify my MySQL using statements. It wants to change this: using (MySqlCommand command = new MySqlCommand(sql_string, connection)) { command.Parameters.AddWithValue("@id", id); MySqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { reader.Read(); business = new Business() { Id = int.Parse(reader["id"].ToString()), Name = reader["name"].ToString(), }; reader.Dispose(); } } Into this: using MySqlCommand

Visual Studio Recommending to Simplify Using Command

半腔热情 提交于 2021-02-04 21:19:13
问题 I've noticed since updating Visual Studio that it is now recommending to simplify my MySQL using statements. It wants to change this: using (MySqlCommand command = new MySqlCommand(sql_string, connection)) { command.Parameters.AddWithValue("@id", id); MySqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { reader.Read(); business = new Business() { Id = int.Parse(reader["id"].ToString()), Name = reader["name"].ToString(), }; reader.Dispose(); } } Into this: using MySqlCommand

Specify NotNull If Method Returns At All

寵の児 提交于 2021-02-04 15:43:05
问题 I'm using the new nullable reference types from C# 8 and I was wondering if it is possible to indicate that a parameter passed in is not null if the method returns at all. I've found [NotNullIf] and [DoesNotReturnIf] but these appear to trigger off a method's return value and a specific bool parameter respectively. Here is what I currently have: public static bool RaiseIfNull([NotNullWhen(true)] object? thing) => RaiseIf(() => thing is null); public static bool RaiseIf(Func<bool> predicate) {

Default interface methods and default values for Auto Properties

安稳与你 提交于 2021-01-27 13:02:24
问题 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? 回答1: Yes. Auto properties aren't a special type of property. They are a convenience feature that generates the code needed to store property values in a backing field. You can specify a default implementation for properties, both for getters and setters. You