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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:20:50+00:00 2026-06-17T21:20:50+00:00

I am trying to generate a constant value that I will be using like

  • 0

I am trying to generate a constant value that I will be using like following:

public class Foo()
{
    public const String ExtensionKey = Guid.NewGuid().ToString();
    public int ID { get; set; }
}

The compiler is throwing an error:

The expression being assigned to 'Foo.ExtensionKey' must be constant

I know that it is not possible to execute a method (a constructor or a type initializer) at compile time. I am looking for a workaround to get randomly generated Guid assigned to different ExtensionKey constants of different classes.

EDIT:

The intention is to generate a UNIQUE Guid per type. The Guid value must be the same for all objects instances and whenever the application run. This is the behavior of Const and I am looking for a way to respect it.

  • 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-17T21:20:51+00:00Added an answer on June 17, 2026 at 9:20 pm

    (Much of this answer “promoted” from a comment to the question.)

    In Visual Studio, you can choose “Tools” – “Create GUID”. Or in Windows PowerShell you can say [Guid]::NewGuid().ToString(). That will give you a Guid. Then you can make the string representation of that particular Guid your string constant.

    public const string ExtensionKey = "2f07b447-f1ba-418b-8065-5571567e63f6";
    

    The Guid is fixed, of course. A field marked const is always static (but you must not supply the static keyword; it is implied).

    If you want to have the field of Guid type, then it can’t be declared const in C#. Then you would do:

    public static readonly Guid ExtensionKey = new Guid("2f07b447-f1ba-418b-8065-5571567e63f6");
    

    readonly means that the field can only be changed from a (static in this case) constructor of the same class (a constructor of a derived class is not OK).

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

Sidebar

Related Questions

I'm trying to implement a class that will generate all possible unordered n-tuples or
I'm trying to generate the following html code using cl-who: <html> <body> <div id=cnt_1></div>
I am trying to embed the following assembly instruction (of an ARM-like architecture) using
Im trying to generate a color gradient using ColdFusion. My current code below works
I'm trying to generate a report of the activities that users have done within
Im am trying to generate interfaces of HP Quality Center OTACLien.dll using com4j. I
I have an enum as follows: public enum SomeType { SOME_KEY (some-display-value-as-label); private String
I am trying to simulate asymmetric key system. I use following code to generate
I am trying to compile this code: class OthelloState { public: // constructor Othello(int
I have a simple class that defines some constants, e.g.: module Foo class Bar

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.