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 7782463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:33:07+00:00 2026-06-01T19:33:07+00:00

I want to restrict creating object using default constructor. Because I have a desing

  • 0

I want to restrict creating object using default constructor. Because I have a desing like below:

class Program
{
    static void Main(string[] args)
    {
        BaseClass bc = new BaseClass("","");
        XmlSerializer xml = new XmlSerializer(typeof(BaseClass));
        StreamWriter sw = new StreamWriter(File.Create("c:\\test.txt"));
        xml.Serialize(sw,bc);
        sw.Flush();
        sw.Close();
    }
}
[Serializable]
public class BaseClass
{
    public string UserName, Password;
    // I don't want to create default constructor because of Authentication
    public BaseClass(string _UserName, string _Password)
    {
        UserName = _UserName;
        Password = _Password;
        f_Authenticate();
    }
    private void f_Authenticate() { }
}

public class DerivedClass:BaseClass
{
    public DerivedClass(string _UserName, string _Password) : base(_UserName, _Password)
    {
    }
}

This is ok. But when I make BaseClass to Serializable it’ll generate this error:
Unhandled Exception: System.InvalidOperationException: ConsoleApplication1.BaseC
lass cannot be serialized because it does not have a parameterless constructor.

Now my design is collapsing because I need to have Username, Password parameters but default constructor is ruining my design….

What should I do?

  • 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-06-01T19:33:10+00:00Added an answer on June 1, 2026 at 7:33 pm

    Create a private default constructor

    private DerivedClass()
    {
        // code
    }
    

    The serialzer will successfully call this even though it’s private

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

Sidebar

Related Questions

I want to restrict the users from using the links in a UIWebView. Hence,
I have a jQuery datepicker that I want to restrict non work days -
We have a server application that we want to restrict non-users from triggerring it
I have a series of interlinked web pages, and I want to restrict access
I have shifts in my application. I want to restrict the time that a
I am creating a Camera application where I want to restrict the camera preview
I have an action I want to restrict only to role Admin. I did
I have a select_date that I want to restrict the months that a user
I want to restrict special numbers for input, so far I have restricted just
I want to restrict access to certain web directories in my website using .htaccess

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.