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 it is possible (in theory) to create a new type at
I know that I can do something like $int = (int)99; //(int) has a
I know that you can insert multiple rows at once, is there a way
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I know that the MsNLB can be configured to user mulitcast with IGMP. However,
I know that IList is the interface and List is the concrete type but
This problem smells like there should be an answer in graph theory, but it
I have a probability problem, which I need to simulate in a reasonable amount
I know that default cron's behavior is to send normal and error output to
I know that .NET is JIT compiled to the architecture you are running on

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.