How do I make sure that the non-parameter constructor is called

兰树豪 381 Reputation points
2023-04-17T03:38:57.3266667+00:00

User's image

Is there any way to do that? Or just copy all the code in to parameter constructor?

Developer technologies ASP.NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2023-04-17T04:58:11.15+00:00

    Try this syntax too:

    public MyClass( int x ) : this( )
    {
       abc = x;
    }
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-04-17T04:08:02.0166667+00:00

    Create a method in your class that both constructors can call.

    2 people found this answer helpful.

  2. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2023-04-17T09:33:51.4366667+00:00

    Hi @兰树豪,
    MyClass is a Class name. so you should use it as a class. User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.