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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:36:32+00:00 2026-06-02T20:36:32+00:00

Just curious, is there a way to have a getter for a constant variable?

  • 0

Just curious, is there a way to have a getter for a constant variable? I have a sort of internal version number to ensure that two versions of a library are still speaking the same language, but I’d like the programmer to be able to check what version they’re using. Right now I use:

 private const Int16 protocol_version = 1;
 public Int16 ProtocolVersion => protocol_version;

But I’d prefer to do it with just the const if there’s a way.

  • 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-02T20:36:36+00:00Added an answer on June 2, 2026 at 8:36 pm

    You could declare a property with only a get accessor (without even declaring the set accessor, not even private):

    private const Int16 protocol_version = 1;
    public Int16 ProtocolVersion => protocol_version;
    

    This is not the same as defining a constant only: the constant would be resolved at compile time, so if you update the library without recompiling the dependent program, the program would still see the "old" value. Consider this example:

    // The class library
    using System;
    
    namespace MyClassLibrary {
        public class X {
            public const Int16 protocol_version = 1;
            public Int16 ProtocolVersion => protocol_version;
        }
    }
    
    // The program
    using System;
    using MyClassLibrary;
    
    class Program {
        static void Main(string[] args) {
            var x = new X();
            Console.WriteLine($"Constant: {X.protocol_version0}");
            Console.WriteLine($"Getter: {x.ProtocolVersion}");
        }
    }
    

    Now, compile the first time and execute the program. You will see

    Constant : 1
    Getter : 1
    

    Then, modify protocol_version to 2, and recompile the class library only, without recompiling the program, then put the new class library in the program folder and execute it. You will see:

    Constant : 1
    Getter : 2
    

    The fact is that if it’s just a constant, the value is replaced at compile time.

    I think that what you are actually looking for is a static readonly variable: in that way, you will avoid the compile-time const replacement, and the variable will not be modifiable after initialization:

    public static readonly Int16 protocol_version = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just curious if there is a way to swap the two stings
I am just curious is there any way to determine if a particular module
Fairly simple; I'm just curious if there's a better way to accomplish this. Given
Just a curious question but is there any programs that can help/aid you when
Just curious - are there various customized Site.css files (and accompanying images) that work
Just curious if there's a way in netbeans to give type hints for regular
Just curious if there is an easy way to load more than one Gemfile
I was just curious if there was a way to issue the following command,
I am just curious if there is an elegant way to solve following problem
Just curious, is there any easy way how go get the list of pages

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.