question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: custom constructors handling

See original GitHub issue

I think it’s a good idea to implement custom constructor support (and analysis). For example, I want the following behavior to work as expected:

    [InitRequired]
    public class Address
    {
        // Default constructor should NOT report CS8618 (since we are using InitRequired).
        public Address() { }

        // This constructor SHOULD report CS8618 (since it doesn't set all non-nullable fields).
        // Even though we have InitRequired attribute set.
        public Address(string city)
        {
            City = city;
        }

        // This constructor should NOT report CS8618 (since it initializes the object in a valid state).
        public Address(string city, bool hasStreet)
        {
            City = city;
            Street = hasStreet ? "Default avenue" : "Unknown";
        }

        public string City { get; set; }
        public string Street { get; set; }
        public string? SecondAddress { get; set; }
    }

    static class Program
    {
        static void Main()
        {
            // CSE001: Missing initialization of all 3 fields.
            var message = new Address();

            // CSE001: Missing initialization of 2 fields.
            var message2 = new Address
            {
                City = "city"
            };

            // Constructor should handle object being properly initialized.
            // Allow custom constuctors here, do NOT report CSE001.
            var message3 = new Address("city");
        }
    }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:25 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
Lonli-Loklicommented, Apr 28, 2021

@cezarypiatek Thank you, it works!

1reaction
cezarypiatekcommented, Apr 16, 2021

It looks like this one has matching constructor argument names to property names. I can make a code fix that will try to rewrite it into init block.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Construction Proposals: Tips and Templates
Using custom brand assets, colors, and other personalized touches can win you bids even if you don't have the lowest offer. Clarify expectations....
Read more >
How to do a construction proposal: examples and template
Key items to include in a construction proposal · The scope of work; · An estimate of the project cost; · The client's...
Read more >
How to Write a Construction Proposal: Free Template ...
As a construction company or contractor, the first step in securing more projects is to craft a compelling construction proposal.
Read more >
How to handle errors in constructors without exceptions?
I'm just going to focus on the part where he said it is sad that C++ constructors require exceptions for error handling.
Read more >
Construction Proposal Software
It's quick and easy to create fully custom proposals with Buildertrend's construction proposal software. Streamline this process effectively to close more ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found