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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:58:31+00:00 2026-05-20T15:58:31+00:00

I know that, in theory, you can not (and should not) derive static classes

  • 0

I know that, in theory, you can not (and should not) derive static classes in C# but I have a case in which I think I need it… I wanted to define a number of static constants for class A and, as I quickly discovered, you can’t do that so I followed this tutorial: http://msdn.microsoft.com/en-us/library/bb397677.aspx

So, I have a static class like this:

public static class ClassAConstants
{
    public const string ConstantA = "constant_a";
    public const string ConstantB = "constant_b";
}

Then, I have class B that extends class A and adds some new static constants. What I would like to do is this:

public static class ClassBConstants : ClassAConstants
{
    public const string ConstantC = "constant_c";
    public const string ConstantD = "constant_d";
}

This way, the four constants would be accessible with ClassBConstants.ConstantA or ClassBConstants.ConstantD. However, C# won’t let me do it.

How can I achieve this? Perhaps the solution is totally different, I don’t care if it does not use static constants at all as long as the result is what I want.


EDIT:

Thanks to Amby I discovered that constants are implicitly static so I really didn’t need to create that artificial static classes (ClassAConstants and ClassBConstants). The solution couldn’t be simpler:

public class A
{
    public const string ConstantA = "constant_a";
    public const string ConstantB = "constant_b";

    // ...

}
public class B : A
{
    public const string ConstantC = "constant_c";
    public const string ConstantD= "constant_d";

    // ...

}

With that code I get the results I wanted initially.

  • 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-20T15:58:31+00:00Added an answer on May 20, 2026 at 3:58 pm

    Choose Singleton instead of static class.
    Then your class benefits from features available to a non-static class, and the user will just have to make the following changes:

    ClassAConstants.ConstantA … to … ClassAConstants.Instance.ConstantA

    By the way, if you are only interested in consts, then the below code would also compile. And then you can access these constants from instance of these classes, or directly by using class name (like accessing static member).

    public class ClassAConstants
    {
        public const string ConstantA = "constant_a";
        public const string ConstantB = "constant_b";
    }
    
    public class ClassBConstants : ClassAConstants
    {
        public const string ConstantC = "constant_c";
        public const string ConstantD = "constant_d";
    }
    

    This is possible since consts are implicitly static.

    ClassAConstants.ConstantA .. works.

    ClassBConstants.ConstantA .. works.

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

Sidebar

Related Questions

I know that lot of questions about HTML sanitizers have appeared in SO, but
I know that similar question was asked several times, but still I can't make
Does anyone know an ORM that can abstract JOINs? I'm using PHP, but I
I know that it is possible (in theory) to create a new type at
I know that all of these will be compiled together into one file but
I know that when linking to multiple static libraries or object files, the order
This problem smells like there should be an answer in graph theory, but it
I feel like I should know this but I've been stumped for hours now
I'm studying information theory but one thing I can't seem to work out. I
I have a Linux device driver that interfaces to a device that, in theory,

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.