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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:07:42+00:00 2026-06-12T16:07:42+00:00

Possible Duplicate: Is there a way to force a C# class to implement certain

  • 0

Possible Duplicate:
Is there a way to force a C# class to implement certain static functions?

Consider a class and consider that, for some reasons, you need to force this class to have a constant inside it. This is to be applied to a whatever set of classes. In my specific case I need some classes of mine to have a constant inside them to hold the corresponding null value.

Those classes that, in my case, implement a certain interface because they must behave in a certain way, must also have three special values that are needed in my context (context that is too long to explain and quite useless here).

So basically I need a way to force my classes to have a certain constant inside them (accessible from outside). I know that I could simply put the constant in those classes, but what if I am looking for a way to tell the compiler: “If this class implements this interface (for example), that you should not compile it if there is not that constant!”.

In this way there is the compiler checking, and not just the programmer who might forget to put the constant.

I hope I could explain my problem. Thanks in advance.

  • 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-12T16:07:44+00:00Added an answer on June 12, 2026 at 4:07 pm

    So basically I need a way to force my classes to have a certain
    constant inside them (accessible from outside). I know that I could
    simply put the constant in those classes, but what if I am looking for
    a way to tell the compiler: “If this class implements this interface
    (for example), that you should not compile it if there is not that
    constant!”.

    You can achieve something like this creating a class containing an unique public constructor which requires all the information/values you need.
    So for example you could use something like:

    class myClass{
       public readonly int Value1;
       public readonly string Value2;
       public myClass(int value1, string value2){
             Value1 = value1;
             Value2 = value2;
       }
    }
    

    Infact in this case the programmer is forced to instantiate the object giving the right values to the unique constructor available, and can’t change the value of the two public fields (due to the readonly keyword).

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

Sidebar

Related Questions

Possible Duplicate: Is there a way to call an object's base class method that's
Possible Duplicate: Returning in a static initializer Is there a way to exit a
Possible Duplicate: Is there some way to compile a .NET application to native code?
Possible Duplicate: Is there any way in C# to override a class method with
Possible Duplicate: Is there any way to force the WorkingSet of a process to
Possible Duplicate: Is there a way to return the id of a row that
Possible Duplicate: is there a way to force event to fire only once on
Possible Duplicate: Limiting variable scope Is there a way to force R to ignore
Possible Duplicate: Is there a way of drawing a caption box in matplotlib Is
Possible Duplicate: Is there any way to determine text direction from CultureInfo in asp.net?

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.