Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3405190
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:28:30+00:00 2026-05-18T05:28:30+00:00

If I define a class with a private default constructor and a public constructor

  • 0

If I define a class with a private default constructor and a public constructor that has parameters, how can I access the private constructor?

public class Bob
{
   public String Surname { get; set; }

   private Bob()
   { }

   public Bob(string surname)
   {
      Surname = surname;
   }
}

I can access the private constructor via a static method on the class like this:

public static Bob GetBob()
{
   return new Bob();
}

I thought that I could access the private constructor via an extension method, since (according to my understanding) extension methods are translated so that they appear to be static methods on the class, but I can’t:

static class Fred
{
   public static Bob Bobby(this Bob bob)
   {
      return new Bob();
   }
}

So, how can I access the private constructor?

Thank you


EDIT:

The reason that I wanted to do this was that I wanted to create tests for one of our business classes, but not allow a consumer of this class to be able to instantiate an object incorrectly. I’m testing it, so I know (I hope!) under what circumstances the tests will fail. I’m still a testing n00b right now so my idea may or may not have been the "wrong way" of doing things.

I’ve changed my testing strategy to just do things the way the a consumer of this class would, i.e. calling the public methods and if the public methods are OK, assuming that the private methods are OK. I would still prefer to test the private methods, but my boss is breathing down my neck on a deliverable 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:28:31+00:00Added an answer on May 18, 2026 at 5:28 am

    New answer (nine years later)

    There is now several overloads for Activator.CreateInstance that allow you to use non public constructors:

    Activator.CreateInstance(typeof(YourClass), true);
    

    true = use non public constructors.

    .

    Old answer

    Default constructors are private for a reason. The developer doesn’t make it private for fun.

    But if you still want to use the default constructor you get it by using reflection.

    var constructor = typeof(Bob).GetConstructor(BindingFlags.NonPublic|BindingFlags.Instance, null, new Type[0], null);
    var instance = (Bob)constructor.Invoke(null);
    

    Edit

    I saw your comment about testing. Never test protected or private methods / properties. You have probably done something wrong if you can’t manage to test those methods/properties through the public API. Either remove them or refactor the class.

    Edit 2

    Forgot a binding flag.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define a class method that has access to a local variable.
Suppose that I define some class: class Pixel { public: Pixel(){ x=0; y=0;}; int
class MyClass { public: ~MyClass() {} MyClass():x(0), y(0){} //default constructor MyClass(int X, int Y):x(X),
I would like to ask, how can I define class inside another one. In
Now I research OOP-part of Scheme. I can define class in Scheme like this:
When you define a class constructor in a base class (i.e. to set some
I want to define a class that is able to populate itself reading from
I want to define a class that supports __getitem__ , but does not allow
take two following classes: class Test1{ public: Test1()=default; Test1(char in1,char in2):char1(in1),char2(in2){} char char1; char
I have a class Date that I defined that models a date, it has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.