29 questions
0 votes
0 answers
47 views
Constructor initializer issue - CS8862 [duplicate]
I was following a video to create small Visual Studio project for an alternate data entry. The video tutorial was on Visual Studio 2019 Blazor project and I'm on 2022 - so some slight variation. Code ...
2 votes
1 answer
93 views
Are variables provided in a primary constructor discarded when only used for initializing properties?
I was trying to figure this out using the Sharplab online C# decompiler but I am not entirely sure if I was understanding the output correctly. Recently in some code I was forwarding an object to some ...
0 votes
1 answer
72 views
How use with Primary constructor bodies?
I saw in Microsoft learn site here option of use primary constractor body with this example: public class C(int i, string s) : B(s) { { if (i < 0) throw new ArgumentOutOfRangeException(...
2 votes
2 answers
97 views
Dependency injection and consumption in C# classes
Say you have public class Garage { public bool AddCar(Car car) { //omitted for brevity } } you add it as services.AddScoped<Garage>(); now you want to inject it into Car class: public ...
0 votes
1 answer
207 views
Primary constructors in the Azure function
I was having a azure function and it's working fine and I have changed that to primary constructor and it's working perfectly fine on my local envrionment but while deploying that using the az ...
3 votes
1 answer
1k views
Do I need underscore in primary constructor parameters' names? [closed]
What is right: class MyClass(IInjectable _injectable) {} or class MyClass(IInjectable injectable) {} On the one hand, we have private field. On the other hand, this is constructor parameter's name.
0 votes
1 answer
140 views
Primary constructor and IDisposable
IDE0290 suggests the use of primary constructors, but if one of the parameters happens to a IDisposable injected and you use readonly backup fields to store them, a CA2213 warning is generated public ...
1 vote
2 answers
218 views
How can a class have a primary constructor and guarantee that its a singleton?
I'l admit that our testing framework would benefit from a significant refactor and this could be solved by better DI. However, I need to work with what I have for now. As part of the testing framework,...
0 votes
2 answers
301 views
What is the intended purpose of declaring a class without a body?
In C# now (I'm not certain when this was added to the spec) we can declare a class without a body: public class MyClass; If I create an instance, though, I can't do anything with it. I thought it ...
0 votes
1 answer
59 views
Enforcing Instance Creation of Record Types Through Specific Methods in C# [duplicate]
I've implemented the Result pattern for error handling in C# using record types, as shown in the code snippet below: namespace WebApi.Utils { public struct Roid { } public record Result<...
10 votes
1 answer
6k views
Warning in property initialization in class with primary constructor
I noticed that a snippet like the below one, it marks the property initialization with a warning. public sealed class C(int a) { public int A { get; } = a; //<--here public int Sum(int b) ...
1 vote
1 answer
1k views
How to initialise a Class using the C#12 Primary Constructor
How do you initialise a class using the C#12 primary construtor syntax. I know how to do it using dependency injection but cant find any docs showing how to do it with a normal class. here is what I ...
0 votes
1 answer
446 views
VS2022 Primary Constructor quick action not complete
Problem When creating a class with a primary constructor, the quick actions for adding another parameter in the constructor do not work as expected. The actual assignment of the constructor parameter ...
15 votes
2 answers
8k views
C# Primary constructor with body?
Can I use a primary constructor and also define a constructor body? Here is my attempt using a helper secondary constructor - I realize this is ambiguous, but I don't know if there is way to fix it... ...
30 votes
4 answers
8k views
Disable recommendation for primary constructors / IDE0290
I am not so happy with primary constructors - because the constructor parameters are not readonly, it's too error prone in my opinion, especially when working with base classes which receive their own ...