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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:58:43+00:00 2026-05-12T21:58:43+00:00

I have a class with several constructors and I want to call the main

  • 0

I have a class with several constructors and I want to call the “main” one from another – but using null.

Using just this(null) results in a compile time error, so I cast null to the type of the other constructor. Compiles fine.

MyClass
{
  public MyClass(SomeType t)
  { }

  public MyClass(IList<FooType> l)
    : this((SomeType)null)
  { }
}

This feels, lets just say icky. Is this okay and common or insane and shows a flaw in the class – in that it should have an empty constructor?

The class “mostly” requires a SomeType, but there are rare times when it is okay to not have one. I want the rare times to “stick out” and be obvious that something “is not a-typical” with the code.

  • 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-12T21:58:44+00:00Added an answer on May 12, 2026 at 9:58 pm

    You need the cast because null by itself is ambiguous. A null by itself has no inherent type, so the cast tells the compiler which constructor you want to pass null to.

    That being said, it is probably more meaningful for your class to use a default (no-arg) constructor, like you said. Having all three constructors is probably okay, too.

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

Sidebar

Related Questions

In one class, I have to call a constructor of another class that needs
I have a base class where I derive several classes. I have another class
I have classes structured like this: Public MustInherit Class A ' several properties End
I have a base class and several subclasses derived from that base class. I
I have a base class Character which has several classes deriving from it. The
I have an parent class, and several child classes. What I want is when
I have a class containing several properties (all are strings if it makes any
I have a class library containing several structures each consisting of several value and
I have a class that currently has several methods that take integer parameters. These
I have a class that is called regularly by several objects. I would like

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.