Questions tagged [c#-9.0]
Use this tag for issues related to development with version 9.0 of the C# language. You should generally also include the [c#] tag.
c#-9.0
266
questions
0
votes
1
answer
58
views
Provide implementation for an abstract method from an interface
Is it possible to override a method implementation by deriving from an interface which gives a default implementation for that method? If not, how should I restructure my code to avoid explicit method ...
3
votes
5
answers
125
views
How to propagate the value of an init-only property to an enclosed class?
I have a Wheel class with two init-only properties Spokes and DiskBrake:
public class Wheel
{
internal Wheel() { }
public int Spokes { get; init; }
public bool DiskBrake { get; init; }
}
...
0
votes
1
answer
131
views
Declaring an init property accessor with null check in C# and without nullable analysis warnings
Recently, I encountered a problem when I tried to declare init accessor for a property in C# with some input validation and enabled nullable reference types. I would like to know if there is a simple ...
2
votes
0
answers
73
views
C# Interlocked Increment/Decrement on Native Int
C# 9.0 introduced native sized integers (see here). However, when trying to use these native integers with Interlocked.Increment() or Interlocked.Decrement() there aren't any overloads that allow this....
0
votes
3
answers
102
views
How do I make my connectionString available to my DataAccess Class using the builder (WebApplication.CreateBuilder)
I have my connection string set in the appsettings.json file and I can see it in the Main using Console.WriteLine so I know I am getting it to the program.cs correctly.
var builder = WebApplication....
2
votes
1
answer
105
views
Why does IndexOf sometimes not work for lists (or collections) of records?
Using the C# 9.0 feature records, I ran into the problem that the IndexOf method of lists (or collections) of records sometimes doesn't seem to work.
This happens in the following example. IndexOf(b) ...
0
votes
0
answers
310
views
Is there a way to make the "with expression" work with classes?
As far as I know, the with expression can only be used with records and structs. The with expression creates a new instance of an object with the specified new values and copying all others (with ...
3
votes
1
answer
2k
views
Does C# 9 support the .NET Framework? [closed]
Similar to Does C# 8 support the .NET Framework?
I know that it's officially unsupported, but what features in C# 9 are available when using the .Net Framework (4.8)?
Features from -- https://learn....
1
vote
1
answer
36
views
Using the value of a default field of Interface inside the classes that implement the interface
I have an interface and I am trying to have a default property in it.
So I tried this:
public interface IMyInterface
{
string DoSomething();
MySpecialClass ClassData {get => new ...
0
votes
1
answer
58
views
Can a child class override a parent's property changing its type?
I'm using Unity, but I think It's not relevant.
I have a base abstract class representing a controller CharacterMovementController and 3 child classes, each one for each axis. I need those 3 to have a ...
6
votes
3
answers
2k
views
Why passing ref struct to methods actually passes a copy of a ref struct?
When I'm trying to pass a ref struct that looks like, say
public ref struct BufferWriter
{
readonly Buffer* _pb;
public int WrittenBytes;
//...
public bool Append(ReadOnlySpan<byte&...
0
votes
1
answer
60
views
How to use linq where condition to filter many keys in a list?
I have a class like this:
class Student
{
public long Id { get; set; }
public long RelatedId { get; set; }
}
I have test below:
var list = ...
6
votes
1
answer
1k
views
Is there a C# attribute on a record field to exclude a field from comparison?
I am getting data with an updated timestamp that I would like to ignore for example:
public record Person
{
public string LastName,
public string FirstName,
public string MiddleName,
[...
2
votes
1
answer
2k
views
How to access the top-level statement variable in a class in C#?
I have a simple code below. I want to access the variable x in the class Program. As x is a global variable, I should be able to access it, Is there a way to access the top-level variable apart from ...
0
votes
1
answer
687
views
Type 'Type' already defines a member called 'Equals' with the same parameter types on Equals(object)
I'm working on a low level class with, amoung other things, custom equality and comparison implementations.
I'm getting the follwoing error:
bool SystemPath.Equals(object obj)
Type 'SystemPath' ...